-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasset.js
More file actions
259 lines (215 loc) · 7.22 KB
/
Copy pathdatasset.js
File metadata and controls
259 lines (215 loc) · 7.22 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection,
RTCSessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription,
RTCIceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate,
getUserMedia = null;
if (navigator.webkitGetUserMedia !== undefined) {
getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
} else if (navigator.mozGetUserMedia !== undefined) {
getUserMedia = navigator.mozGetUserMedia.bind(navigator);
}
var DEFAULT_STUN_SERVER = {'url': 'stun:stun.l.google.com:19302'},
_socket = io.connect('http://patjameson.com:3000/'),
_roomId = '',
_pcs = [],
_dcs = [],
_commandChannel = [],
onmessagecallback = function() {},
/**
* If a download has started, _downloadStatus will be the number of chunks that
* remain to be recieved.
*/
_downloadStatus = 0,
/**
* The actual data in the downloaded file. It is appended each time a chunk is
* recieved.
*/
_download = '',
_downloadName = '';
/**
* Broadcasts a message to the room.
*
* @param message {String} The message to be broadcasted.
* @param dcs {Array} array of DataChannels to send the message to.
*/
function send(message) {
var i;
for (i = 0;i < _dcs.length;i++) {
if (_dcs[i] !== undefined && _dcs[i].readyState == "open") {
_dcs[i].send(message);
}
}
}
function sendCommand(message) {
var i;
for (i = 0;i < _commandChannel.length;i++) {
if (_commandChannel[i] !== undefined && _commandChannel[i].readyState == "open") {
console.log(message);
_commandChannel[i].send(message);
}
}
}
var temp_messages = [];
/**
* Broadcasts a file download to the room given a file input element
*
* @param id {String} The id of the file input element.
*/
function sendLarge(message) {
var connected = true;
for (var i = 0;i < _dcs.length;i++) {
if (_dcs[i] !== undefined && _dcs[i].readyState != "open")
connected = false;
}
if (connected) {
var size = message.length,
chunkSize = 1000,
numChunks = Math.ceil(size / chunkSize),
j = 0,
i;
send('start' + numChunks);
sendChunk(message, chunkSize, 0, numChunks);
} else {
temp_messages.push(message);
}
}
function sendChunk(message, chunkSize, chunk, maxChunks) {
send(message.substring(chunk*chunkSize, chunk*chunkSize + chunkSize));
if (chunk < maxChunks) {
setTimeout(function () {
sendChunk(message, chunkSize, chunk+1, maxChunks);
}, 400);
}
}
function _startDataChannel (id) {
_dcs[id] = _pcs[id].createDataChannel('file', {'binaryType': 'blob'});
_dcs[id].onmessage = function(message) {
console.log('RAW' + message.data);
console.log('STATUS: ' + _downloadStatus);
if (_downloadStatus > 0) {
_download += message.data;
_downloadStatus--;
console.log('STAT: ' + _downloadStatus);
if (_downloadStatus == 0) {
console.log(_download);
_download = '';
}
} else {
console.log('WO' + message.data.substring(0, 5));
if (message.data.substring(0, 5) == 'start') {
var splitComma = message.data.indexOf(',');
_downloadStatus = parseInt(message.data.substring(5), 10);
console.log(message.data.substring(5));
}
}
};
_commandChannel[id] = _pcs[id].createDataChannel('commands', {'binaryType': 'blob'});
_commandChannel[id].onmessage = onmessagecallback;
}
/**
* Creates a connection between two peer connections, starts a datachannel, and adds the localStream.
*
* @param id {String} the id of the local peer connection
*/
function _createConn (id) {
var configuration = {'iceServers': [DEFAULT_STUN_SERVER]},
connection = null;
connection = {'optional': [{'RtpDataChannels': true}]};
_pcs[id] = new RTCPeerConnection(configuration, connection);
_startDataChannel(id);
}
function makeConnection (roomId, callback) {
console.log('wat');
//the server calls to signify that it knows what room client is in.
_socket.on('joined', function (data) {
console.log('test');
console.log(data);
client_id = data.client_id;
id = data.id;
//is where the connection 'chaining' starts.
if (client_id !== 0) {
_createConn(0);
_updateDescription(_pcs[0], client_id, 0);
}
console.log('omg');
callback(id);
});
_socket.on('add_desc', function (data) {
var client_id = data.from_client_id,
id = data.id,
owner = false;
console.log(data.client_id + " <------ " + client_id);
if (_pcs[client_id] === undefined) {
_createConn(client_id);
owner = true;
}
//called when we recieve an ice candidate from the other client
var exp = 1;
_pcs[client_id].onicecandidate = function (event) {
if (event.candidate) {
_socket.emit('cand', {
'client_id': client_id,
'from_client_id': data.client_id,
'cand': event.candidate,
'id': id
});
console.log(exp);
exp++;
}
};
_pcs[client_id].setRemoteDescription(new RTCSessionDescription(data.desc));
if (owner) {
_pcs[client_id].createAnswer(function (desc) {
_pcs[client_id].setLocalDescription(desc);
console.log(data.client_id + " ------> " + data.from_client_id);
_socket.emit('desc', {
'client_id': data.from_client_id,
'from_client_id': data.client_id,
'desc': desc,
'id': id
});
});
}
});
_socket.on('add_cand', function (data) {
if (_pcs[data.client_id] === undefined) {
if (cands[data.client_id] === undefined) {
cands[data.client_id] = [];
}
cands[data.client_id].push(data.cand);
} else {
_pcs[data.client_id].addIceCandidate(new RTCIceCandidate(data.cand));
}
setTimeout(function() {
console.log('here');
checkConn(data.client_id);
}, 2000);
});
console.log(roomId);
//tell the server what room to join.
_socket.emit('join', {
'id': roomId
});
}
function _updateDescription(pc, client_id2, _client_id) {
var thisWebRTC = this;
pc.createOffer(function (desc) {
pc.setLocalDescription(desc);
console.log(client_id2 + " ------> " + _client_id);
thisWebRTC._socket.emit('desc', {
'client_id': _client_id,
'from_client_id': client_id2,
'desc': desc,
'id': id
});
}, null);
}
function checkConn(dc_id) {
if (_dcs[dc_id].readyState != "open") {
_updateDescription(_pcs[dc_id], client_id, dc_id);
setTimeout(function() {
checkConn(dc_id);
}, 1000);
} else {
console.log("ASKING FOR KEYS");
}
}