|
1 | 1 | /* MIT License: https://webrtc-experiment.appspot.com/licence/
|
2 |
| - 2013, Muaz Khan<muazkh>--[github.com/muaz-khan] |
3 |
| - |
4 |
| - https://github.com/muaz-khan/WebRTC-Experiment/tree/master/DataChannel |
5 |
| -*/ |
| 2 | + 2013, Muaz Khan<muazkh>--[github.com/muaz-khan] |
| 3 | +
|
| 4 | + https://github.com/muaz-khan/WebRTC-Experiment/tree/master/DataChannel |
| 5 | + */ |
6 | 6 | (function () {
|
7 | 7 | window.DataChannel = function (channel, extras) {
|
8 | 8 | if (channel) this.automatic = true;
|
|
50 | 50 |
|
51 | 51 | self.openSignalingChannel = function (config) {
|
52 | 52 | config = config || {};
|
| 53 | + |
53 | 54 | channel = config.channel || self.channel || 'default-channel';
|
54 | 55 | var socket = new window.Firebase('https://' + (extras.firebase || self.firebase || 'chat') + '.firebaseIO.com/' + channel);
|
55 | 56 | socket.channel = channel;
|
56 | 57 |
|
57 | 58 | socket.on('child_added', function (data) {
|
58 | 59 | var value = data.val();
|
59 |
| - if (value == 'joking') config.onopen && config.onopen(); |
60 |
| - else if (value == 'joking-again') { |
| 60 | + |
| 61 | + if (value == 'open-default-socket') { |
61 | 62 | self.onDefaultSocketOpened && self.onDefaultSocketOpened();
|
62 | 63 | }
|
63 | 64 | else config.onmessage(value);
|
|
67 | 68 | this.push(data);
|
68 | 69 | };
|
69 | 70 |
|
70 |
| - socket.push('joking'); |
71 |
| - |
72 | 71 | if (!self.socket) self.socket = socket;
|
| 72 | + if (channel != self.channel || (self.isInitiator && channel == self.channel)) |
| 73 | + socket.onDisconnect().remove(); |
| 74 | + |
| 75 | + if (config.onopen) setTimeout(config.onopen, 1); |
| 76 | + |
73 | 77 | return socket;
|
74 | 78 | };
|
75 | 79 |
|
|
85 | 89 | callback();
|
86 | 90 | verifySocketConnection();
|
87 | 91 | }
|
88 |
| - } else callback(); |
| 92 | + } else { |
| 93 | + callback(); |
| 94 | + verifySocketConnection(); |
| 95 | + } |
89 | 96 | }
|
90 | 97 |
|
91 | 98 | function verifySocketConnection() {
|
92 | 99 | 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 |
| - } |
| 100 | + new window.Firebase('https://' + (extras.firebase || self.firebase || 'chat') + '.firebaseIO.com/.info/connected').on('value', function (snap) { |
| 101 | + if (snap.val() === true && self.socket) self.socket.send('open-default-socket'); |
99 | 102 | });
|
100 | 103 | }
|
| 104 | + else if (self.onDefaultSocketOpened) self.onDefaultSocketOpened(); |
101 | 105 | }
|
102 | 106 |
|
103 | 107 | function init() {
|
|
171 | 175 |
|
172 | 176 | this.open = function (_channel) {
|
173 | 177 | self.joinedARoom = true;
|
174 |
| - if (_channel) self.channel = _channel; |
175 | 178 |
|
176 |
| - setDefaults(true); |
| 179 | + if (self.socket) self.socket.onDisconnect().remove(); |
| 180 | + else self.isInitiator = true; |
| 181 | + |
| 182 | + if (_channel) self.channel = _channel; |
177 | 183 |
|
178 | 184 | prepareInit(function () {
|
179 | 185 | init();
|
180 | 186 | if (IsDataChannelSupported) dataConnector.createRoom();
|
181 | 187 | });
|
182 |
| - |
183 |
| - if (self.socket) self.socket.onDisconnect().remove(); |
184 | 188 | };
|
185 | 189 |
|
186 | 190 | this.connect = function (_channel) {
|
187 | 191 | if (_channel) self.channel = _channel;
|
188 |
| - setDefaults(false); |
189 | 192 | prepareInit(init);
|
190 | 193 | };
|
191 | 194 |
|
|
230 | 233 | };
|
231 | 234 |
|
232 | 235 | 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 |
| - |
246 | 236 | dataConnector.leave(userid);
|
247 | 237 | };
|
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 |
| - } |
262 | 238 | };
|
263 | 239 |
|
264 | 240 | window.moz = !!navigator.mozGetUserMedia;
|
|
271 | 247 | IceCandidate = w.mozRTCIceCandidate || w.RTCIceCandidate;
|
272 | 248 |
|
273 | 249 | var iceServers = {
|
274 |
| - iceServers: [{ |
275 |
| - url: !moz ? 'stun:stun.l.google.com:19302' : 'stun:23.21.150.121' |
276 |
| - }] |
| 250 | + iceServers: [ |
| 251 | + { |
| 252 | + url: !moz ? 'stun:stun.l.google.com:19302' : 'stun:23.21.150.121' |
| 253 | + } |
| 254 | + ] |
277 | 255 | };
|
278 | 256 |
|
279 | 257 | var optional = {
|
280 | 258 | optional: []
|
281 | 259 | };
|
282 | 260 |
|
283 | 261 | if (!moz) {
|
284 |
| - optional.optional = [{ |
285 |
| - RtpDataChannels: true |
286 |
| - }]; |
| 262 | + optional.optional = [ |
| 263 | + { |
| 264 | + RtpDataChannels: true |
| 265 | + } |
| 266 | + ]; |
287 | 267 | }
|
288 | 268 |
|
289 | 269 | var peerConnection = new PeerConnection(iceServers, optional);
|
|
393 | 373 | }
|
394 | 374 | }
|
395 | 375 |
|
396 |
| - function useless() { } |
| 376 | + function useless() { |
| 377 | + } |
397 | 378 |
|
398 | 379 | return {
|
399 | 380 | addAnswerSDP: function (sdp) {
|
|
417 | 398 |
|
418 | 399 | function DataConnector(config) {
|
419 | 400 | var self = {
|
420 |
| - userToken: uniqueToken(), |
421 |
| - sockets: [], |
422 |
| - socketObjects: {} |
423 |
| - }, |
| 401 | + userToken: uniqueToken(), |
| 402 | + sockets: [], |
| 403 | + socketObjects: {} |
| 404 | + }, |
424 | 405 | channels = '--',
|
425 | 406 | isbroadcaster,
|
426 | 407 | isGetNewRoom = true,
|
|
0 commit comments