-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti.js
154 lines (137 loc) · 4.12 KB
/
multi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Connect to PeerJS, have server assign an ID instead of providing one
// Showing off some of the configs available with PeerJS :).
var peer = new Peer({
// Set API key for cloud server (you don't need this if you're running your
// own.
key: 'lwjd5qra8257b9',
//key: 'peerjs', host: 'localhost', port:9000,
// Use a TURN server for more network support
config: {'iceServers': [
{ url: 'stun:stun.l.google.com:19302' }
]} /* Sample servers, please use appropriate ones */
});
var connectedPeers = {};
// Show this peer's ID.
peer.on('open', function(id){
$('#pid').text(id);
});
// Await connections from others
peer.on('connection', connect);
// Handle a connection object.
function connect(c) {
// Handle a chat connection.
if (c.label === 'chat') {
var chatbox = $('<div></div>').addClass('connection').addClass('active').attr('id', c.peer);
var header = $('<h1></h1>').html('Chat with <strong>' + c.peer + '</strong>');
var messages = $('<div><em>Peer connected.</em></div>').addClass('messages');
chatbox.append(header);
chatbox.append(messages);
// Select connection handler.
chatbox.on('click', function() {
if ($(this).attr('class').indexOf('active') === -1) {
$(this).addClass('active');
} else {
$(this).removeClass('active');
}
});
$('.filler').hide();
$('#connections').append(chatbox);
c.on('data', function(data) {
messages.append('<div><span class="peer">' + c.peer + '</span>: ' + data +
'</div>');
});
c.on('close', function() {
alert(c.peer + ' has left the chat.');
chatbox.remove();
if ($('.connection').length === 0) {
$('.filler').show();
}
delete connectedPeers[c.peer];
});
} else if (c.label === 'game') {
c.on('data', function(data) {
console.log("rx:"+data);
game_cmd(c,data);
});
}
}
$(document).ready(function() {
if (util.supports.data) {
$('#webrtc').show();
}
// Connect to a peer
$('#connect').click(function() {
requestedPeer = $('#rid').val();
if (!connectedPeers[requestedPeer]) {
// Create 2 connections, one labelled chat and another labelled game.
var c = peer.connect(requestedPeer, {
label: 'chat',
serialization: 'none',
reliable: false,
metadata: {message: 'hi i want to chat with you!'}
});
c.on('open', function() {
connect(c);
});
c.on('error', function(err) { alert(err); });
var f = peer.connect(requestedPeer, { label: 'game' });
f.on('open', function() {
connect(f);
});
f.on('error', function(err) { alert(err); });
}
connectedPeers[requestedPeer] = 1;
});
// Close a connection.
$('#close').click(function() {
eachActiveConnection(function(c) {
c.close();
});
});
// Send a chat message to all active connections.
$('#send').submit(function(e) {
e.preventDefault();
// For each active connection, send the message.
var msg = $('#text').val();
eachActiveConnection(function(c, $c) {
if (c.label === 'chat') {
c.send(msg);
$c.find('.messages').append('<div><span class="you">You: </span>' + msg
+ '</div>');
}
});
$('#text').val('');
$('#text').focus();
});
});
function game_cmd_send(data)
{
console.log("tx:"+data);
eachActiveConnection(function(c, $c) {
if (c.label === 'game') {
c.send(data);
}
});
}
// Goes through each active peer and calls FN on its connections.
function eachActiveConnection(fn) {
var actives = $('.active');
var checkedIds = {};
actives.each(function() {
var peerId = $(this).attr('id');
if (!checkedIds[peerId]) {
var conns = peer.connections[peerId];
for (var i = 0, ii = conns.length; i < ii; i += 1) {
var conn = conns[i];
fn(conn, $(this));
}
}
checkedIds[peerId] = 1;
});
}
// Make sure things clean up properly.
window.onunload = window.onbeforeunload = function(e) {
if (!!peer && !peer.destroyed) {
peer.destroy();
}
};