Skip to content

Commit 386ef95

Browse files
committed
Bugs fixed: DataChannel.js and RTCMultiConnection.js
https://webrtc-experiment.appspot.com/#DataChannel https://webrtc-experiment.appspot.com/#RTCMultiConnection It was a bug which is fixed now. Two unique sessions was opened for initiator. 1) First session was playing role of participant --- this session is auto opened 2) Second was playing role of initiator --- this session is manually opened by the user Now, RTCMultiConnection-v1.2.js only opens one-session all the time. Test it: https://webrtc-experiment.appspot.com/RTCMultiConnection/users-ejection/
1 parent bada57c commit 386ef95

18 files changed

+406
-224
lines changed

DataChannel/DataChannel.js

+109-48
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/* MIT License: https://webrtc-experiment.appspot.com/licence/
2-
2013, Muaz Khan<muazkh>--[ github.com/muaz-khan ]
2+
2013, Muaz Khan<muazkh>--[github.com/muaz-khan]
33
44
https://github.com/muaz-khan/WebRTC-Experiment/tree/master/DataChannel
55
*/
6-
76
(function () {
87
window.DataChannel = function (channel, extras) {
98
if (channel) this.automatic = true;
@@ -19,7 +18,7 @@
1918
};
2019

2120
this.channels = {};
22-
this.onopen = function (userid/*, _channel */) {
21+
this.onopen = function (userid) {
2322
self.send(userid, 'is connected with you.');
2423
};
2524

@@ -54,30 +53,56 @@
5453
channel = config.channel || self.channel || 'default-channel';
5554
var socket = new window.Firebase('https://' + (extras.firebase || self.firebase || 'chat') + '.firebaseIO.com/' + channel);
5655
socket.channel = channel;
56+
5757
socket.on('child_added', function (data) {
5858
var value = data.val();
5959
if (value == 'joking') config.onopen && config.onopen();
60+
else if (value == 'joking-again') {
61+
self.onDefaultSocketOpened && self.onDefaultSocketOpened();
62+
}
6063
else config.onmessage(value);
6164
});
65+
6266
socket.send = function (data) {
6367
this.push(data);
6468
};
69+
6570
socket.push('joking');
66-
67-
self.socket = socket;
71+
72+
if (!self.socket) self.socket = socket;
6873
return socket;
6974
};
7075

7176
if (!window.Firebase) {
7277
var script = document.createElement('script');
7378
script.src = 'https://cdn.firebase.com/v0/firebase.js';
74-
script.onload = callback;
79+
script.onload = function () {
80+
callback();
81+
verifySocketConnection();
82+
};
7583
document.documentElement.appendChild(script);
76-
} else callback();
84+
} else {
85+
callback();
86+
verifySocketConnection();
87+
}
7788
} else callback();
7889
}
7990

91+
function verifySocketConnection() {
92+
if (window.Firebase) {
93+
// to check if firebase is connected
94+
var isFirebaseConnected = new window.Firebase('https://' + (extras.firebase || self.firebase || 'chat') + '.firebaseIO.com/.info/connected');
95+
isFirebaseConnected.on('value', function (snap) {
96+
if (snap.val() === true) {
97+
if (self.socket) self.socket.send('joking-again');
98+
}
99+
});
100+
}
101+
}
102+
80103
function init() {
104+
if (self.config) return;
105+
81106
self.config = {
82107
openSocket: function (config) {
83108
return self.openSignalingChannel(config);
@@ -130,18 +155,15 @@
130155
self.onFileProgress(packets);
131156
},
132157
channel: self.channel,
133-
onDefaultSocketOpened: function () {
134-
if (self.onDefaultSocketOpened) self.onDefaultSocketOpened();
135-
},
136158
onleave: function (userid) {
137159
self.onleave(userid);
138160
},
139161
transmitRoomOnce: !!extras.transmitRoomOnce
140162
};
141163

142164
dataConnector = IsDataChannelSupported ?
143-
new DataConnector(self.config) :
144-
new SocketConnector(self.config);
165+
new DataConnector(self.config) :
166+
new SocketConnector(self.config);
145167

146168
fileReceiver = new FileReceiver();
147169
textReceiver = new TextReceiver();
@@ -150,29 +172,32 @@
150172
this.open = function (_channel) {
151173
self.joinedARoom = true;
152174
if (_channel) self.channel = _channel;
175+
176+
setDefaults(true);
177+
153178
prepareInit(function () {
154179
init();
155180
if (IsDataChannelSupported) dataConnector.createRoom();
156181
});
157-
if(self.socket) self.socket.onDisconnect().remove();
182+
183+
if (self.socket) self.socket.onDisconnect().remove();
158184
};
185+
159186
this.connect = function (_channel) {
160187
if (_channel) self.channel = _channel;
188+
setDefaults(false);
161189
prepareInit(init);
162190
};
163191

164-
// auto creating/joing rooms
165192
if (this.automatic) {
166193
this.connect();
167194
this.onDefaultSocketOpened = function () {
168195
if (self.isDefaultSocketOpened) return;
169196
self.isDefaultSocketOpened = true;
170197

171-
if (!self.joinedARoom)
172-
// wait 5 seconds for pre-created room; otherwise create new one
173-
setTimeout(function () {
174-
if (!self.joinedARoom) self.open();
175-
}, 5000);
198+
if (!self.joinedARoom) setTimeout(function () {
199+
if (!self.joinedARoom) self.open();
200+
}, 1500);
176201
};
177202
}
178203

@@ -205,8 +230,35 @@
205230
};
206231

207232
this.leave = function (userid) {
233+
if (typeof userid === 'function') {
234+
var callback = userid;
235+
userid = null;
236+
}
237+
238+
if (!userid) {
239+
dataConnector.leaving = true;
240+
if (callback) (function looper() {
241+
if (dataConnector.left) callback();
242+
else setTimeout(looper, 100);
243+
})();
244+
}
245+
208246
dataConnector.leave(userid);
209247
};
248+
249+
function setDefaults(isInitiator) {
250+
self.defaults = {
251+
isInitiator: isInitiator
252+
};
253+
254+
self.reconnect = function () {
255+
if (self.joinedARoom) self.leave();
256+
self.joinedARoom = false;
257+
258+
if (self.defaults.isInitiator) self.open();
259+
else self.connect();
260+
};
261+
}
210262
};
211263

212264
window.moz = !!navigator.mozGetUserMedia;
@@ -297,10 +349,10 @@
297349

298350
function _openOffererChannel() {
299351
channel = peerConnection.createDataChannel(
300-
options.channel || 'RTCDataChannel',
301-
moz ? {} : {
302-
reliable: false
303-
});
352+
options.channel || 'RTCDataChannel',
353+
moz ? {} : {
354+
reliable: false
355+
});
304356
if (moz) channel.binaryType = 'blob';
305357
setChannelEvents();
306358
}
@@ -317,7 +369,6 @@
317369
if (options.onChannelClosed) options.onChannelClosed(event);
318370
};
319371
channel.onerror = function (event) {
320-
console.error(event);
321372
if (options.onChannelError) options.onChannelError(event);
322373
};
323374
}
@@ -342,8 +393,7 @@
342393
}
343394
}
344395

345-
function useless() {
346-
}
396+
function useless() { }
347397

348398
return {
349399
addAnswerSDP: function (sdp) {
@@ -378,10 +428,7 @@
378428

379429
function openDefaultSocket() {
380430
defaultSocket = config.openSocket({
381-
onmessage: onDefaultSocketResponse,
382-
onopen: function () {
383-
if (config.onDefaultSocketOpened) config.onDefaultSocketOpened();
384-
}
431+
onmessage: onDefaultSocketResponse
385432
});
386433
}
387434

@@ -395,7 +442,7 @@
395442
if (response.userToken && response.joinUser == self.userToken && response.participant && channels.indexOf(response.userToken) == -1) {
396443
channels += response.userToken + '--';
397444

398-
console.debug('A person whose id is', response.userToken || response.channel, 'particiated with me!');
445+
console.debug('A person whose id is', response.userToken || response.channel, 'is trying to share data with me!');
399446
openSubSocket({
400447
isofferer: true,
401448
channel: response.channel || response.userToken,
@@ -418,10 +465,10 @@
418465
};
419466

420467
var socket = config.openSocket(socketConfig),
421-
isofferer = _config.isofferer,
422-
gotstream,
423-
inner = {},
424-
peer;
468+
isofferer = _config.isofferer,
469+
gotstream,
470+
inner = {},
471+
peer;
425472

426473
var peerConfig = {
427474
onICE: function (candidate) {
@@ -459,17 +506,12 @@
459506
if (config.onChannelOpened) config.onChannelOpened(_config.userid, channel);
460507

461508
if (config.direction === 'many-to-many' && isbroadcaster && channels.split('--').length > 3) {
462-
463-
console.debug('It is time to transmit participant\'s details: ', socket.channel);
464-
465509
defaultSocket.send({
466510
newParticipant: socket.channel,
467511
userToken: self.userToken
468512
});
469513
}
470514

471-
//if (_config.closeSocket) socket = null;
472-
473515
window.isFirstConnectionOpened = gotstream = true;
474516
}
475517

@@ -505,7 +547,7 @@
505547
function socketResponse(response) {
506548
if (response.userToken == self.userToken) return;
507549

508-
console.log(response);
550+
console.debug('private socket:', response);
509551

510552
if (response.firstPart || response.secondPart || response.thirdPart) {
511553
if (response.firstPart) {
@@ -540,7 +582,7 @@
540582
}
541583

542584
if (response.closeEntireSession) {
543-
// room owner asked me to leave his room
585+
// room owner is trying to close the entire session
544586
leaveChannels();
545587
} else if (socket) {
546588
socket.send({
@@ -609,6 +651,17 @@
609651
delete sockets[i];
610652
}
611653
}
654+
655+
/* closing all RTCDataChannels */
656+
length = RTCDataChannels.length;
657+
for (i = 0; i < length; i++) {
658+
var _channel = RTCDataChannels[i];
659+
if (_channel) {
660+
_channel.close();
661+
delete RTCDataChannels[i];
662+
}
663+
}
664+
that.left = true;
612665
}
613666

614667
// eject a specific user!
@@ -621,6 +674,8 @@
621674
}
622675
}
623676

677+
var that = this;
678+
624679
window.onunload = function () {
625680
leaveChannels();
626681
};
@@ -639,7 +694,7 @@
639694
broadcaster: self.userToken
640695
});
641696

642-
if (!config.transmitRoomOnce) {
697+
if (!config.transmitRoomOnce && !that.leaving) {
643698
if (config.direction === 'one-to-one') {
644699
if (!window.isFirstConnectionOpened) setTimeout(transmit, 3000);
645700
} else setTimeout(transmit, 3000);
@@ -662,7 +717,7 @@
662717
},
663718
send: function (message, _channel) {
664719
var _channels = RTCDataChannels,
665-
data, length = _channels.length;
720+
data, length = _channels.length;
666721
if (!length) return;
667722

668723
if (moz && message.file) data = message.file;
@@ -672,7 +727,13 @@
672727
else for (var i = 0; i < length; i++)
673728
_channels[i].send(data);
674729
},
675-
leave: leaveChannels
730+
leave: function (userid) {
731+
leaveChannels(userid);
732+
if (!userid) {
733+
self.joinedARoom = isbroadcaster = false;
734+
isGetNewRoom = true;
735+
}
736+
}
676737
};
677738
}
678739

@@ -698,8 +759,8 @@
698759
var FileSender = {
699760
send: function (config) {
700761
var channel = config.channel,
701-
_channel = config._channel,
702-
file = config.file;
762+
_channel = config._channel,
763+
file = config.file;
703764

704765
/* if firefox nightly: share file blob directly */
705766
if (moz && IsDataChannelSupported) {
@@ -831,7 +892,7 @@
831892
var TextSender = {
832893
send: function (config) {
833894
var channel = config.channel,
834-
_channel = config._channel,
895+
_channel = config._channel,
835896
initialText = config.text,
836897
packetSize = 1000 /* chars */,
837898
textToTransfer = '',

RTCMultiConnection/RTCMultiConnection-Demos/all-in-one.html

+7-11
Original file line numberDiff line numberDiff line change
@@ -426,17 +426,13 @@ <h2>
426426

427427
<br/>
428428
<br/>
429-
<table style="width: 100%;">
430-
<tr>
431-
<td>
432-
<div>
433-
<textarea id="message" style="height: 8em; width: 98%; margin: .2em;"
434-
placeholder="Have any message? Suggestions or something went wrong?"></textarea>
435-
</div>
436-
<button id="send-message" class="send-message" style="font-size: 1em;">Send Message</button>
437-
</td>
438-
</tr>
439-
</table>
429+
<section style="border: 1px solid rgb(189, 189, 189); margin: 1em 3em; border-radius: .2em;">
430+
<h2 id="feedback" style="padding: .2em .4em; border-bottom: 1px solid rgb(189, 189, 189);">Feedback</h2>
431+
<div>
432+
<textarea id="message" style="height: 8em; margin: .2em; width: 98%; border: 1px solid rgb(189, 189, 189); outline: none; resize: vertical;" placeholder="Have any message? Suggestions or something went wrong?"></textarea>
433+
</div>
434+
<button id="send-message" style="font-size: 1em;">Send Message</button>
435+
</section>
440436
</article>
441437
<footer>
442438
<p>

0 commit comments

Comments
 (0)