|
@@ -856,11 +856,13 @@ function Peer(options) {
|
|
this._server = options.host + ':' + options.port;
|
|
this._server = options.host + ':' + options.port;
|
|
|
|
|
|
// Ensure alphanumeric_-
|
|
// Ensure alphanumeric_-
|
|
- if (options.id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.id))
|
|
|
|
|
|
+ if (options.id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.id)) {
|
|
throw new Error('Peer ID can only contain alphanumerics, "_", and "-".');
|
|
throw new Error('Peer ID can only contain alphanumerics, "_", and "-".');
|
|
- if (options.key && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.key))
|
|
|
|
|
|
+ }
|
|
|
|
+ if (options.key && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.key)) {
|
|
throw new Error('API key can only contain alphanumerics, "_", and "-".');
|
|
throw new Error('API key can only contain alphanumerics, "_", and "-".');
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
this._id = options.id;
|
|
this._id = options.id;
|
|
// Not used unless using cloud server.
|
|
// Not used unless using cloud server.
|
|
this._key = options.key;
|
|
this._key = options.key;
|
|
@@ -887,8 +889,9 @@ Peer.prototype._startSocket = function() {
|
|
});
|
|
});
|
|
this._socket.on('unavailable', function(peer) {
|
|
this._socket.on('unavailable', function(peer) {
|
|
util.log('Destination peer not available.', peer);
|
|
util.log('Destination peer not available.', peer);
|
|
- if (self.connections[peer])
|
|
|
|
|
|
+ if (self.connections[peer]) {
|
|
self.connections[peer].close();
|
|
self.connections[peer].close();
|
|
|
|
+ }
|
|
});
|
|
});
|
|
this._socket.on('error', function(error) {
|
|
this._socket.on('error', function(error) {
|
|
util.log(error);
|
|
util.log(error);
|
|
@@ -929,19 +932,25 @@ Peer.prototype._handleServerJSONMessage = function(message) {
|
|
this.connections[peer] = connection;
|
|
this.connections[peer] = connection;
|
|
break;
|
|
break;
|
|
case 'ANSWER':
|
|
case 'ANSWER':
|
|
- if (connection) connection.handleSDP(message);
|
|
|
|
|
|
+ if (connection) {
|
|
|
|
+ connection.handleSDP(message);
|
|
|
|
+ }
|
|
break;
|
|
break;
|
|
case 'CANDIDATE':
|
|
case 'CANDIDATE':
|
|
- if (connection) connection.handleCandidate(message);
|
|
|
|
|
|
+ if (connection) {
|
|
|
|
+ connection.handleCandidate(message);
|
|
|
|
+ }
|
|
break;
|
|
break;
|
|
case 'LEAVE':
|
|
case 'LEAVE':
|
|
- if (connection) connection.handleLeave();
|
|
|
|
|
|
+ if (connection) {
|
|
|
|
+ connection.handleLeave();
|
|
|
|
+ }
|
|
break;
|
|
break;
|
|
case 'PORT':
|
|
case 'PORT':
|
|
- if (util.browserisms === 'Firefox') {
|
|
|
|
- connection.handlePort(message);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
|
|
+ //if (util.browserisms === 'Firefox') {
|
|
|
|
+ // connection.handlePort(message);
|
|
|
|
+ // break;
|
|
|
|
+ //}
|
|
case 'DEFAULT':
|
|
case 'DEFAULT':
|
|
util.log('Unrecognized message type:', message.type);
|
|
util.log('Unrecognized message type:', message.type);
|
|
break;
|
|
break;
|
|
@@ -958,10 +967,9 @@ Peer.prototype._processQueue = function() {
|
|
|
|
|
|
|
|
|
|
Peer.prototype._cleanup = function() {
|
|
Peer.prototype._cleanup = function() {
|
|
- for (var peer in this.connections) {
|
|
|
|
- if (this.connections.hasOwnProperty(peer)) {
|
|
|
|
- this.connections[peer].close();
|
|
|
|
- }
|
|
|
|
|
|
+ var peers = Object.keys(this.connections);
|
|
|
|
+ for (var i = 0, ii = peers.length; i < ii; i++) {
|
|
|
|
+ this.connections[peers[i]].close();
|
|
}
|
|
}
|
|
this._socket.close();
|
|
this._socket.close();
|
|
};
|
|
};
|
|
@@ -970,7 +978,9 @@ Peer.prototype._cleanup = function() {
|
|
Peer.prototype._attachConnectionListeners = function(connection) {
|
|
Peer.prototype._attachConnectionListeners = function(connection) {
|
|
var self = this;
|
|
var self = this;
|
|
connection.on('close', function(peer) {
|
|
connection.on('close', function(peer) {
|
|
- if (self.connections[peer]) delete self.connections[peer];
|
|
|
|
|
|
+ if (self.connections[peer]) {
|
|
|
|
+ delete self.connections[peer];
|
|
|
|
+ }
|
|
});
|
|
});
|
|
};
|
|
};
|
|
|
|
|
|
@@ -1026,9 +1036,9 @@ function DataConnection(id, peer, socket, cb, options) {
|
|
this._socket = socket;
|
|
this._socket = socket;
|
|
|
|
|
|
// Firefoxism: connectDataConnection ports.
|
|
// Firefoxism: connectDataConnection ports.
|
|
- if (util.browserisms === 'Firefox') {
|
|
|
|
|
|
+ /*if (util.browserisms === 'Firefox') {
|
|
this._firefoxPortSetup();
|
|
this._firefoxPortSetup();
|
|
- }
|
|
|
|
|
|
+ }*/
|
|
|
|
|
|
// Set up PeerConnection.
|
|
// Set up PeerConnection.
|
|
this._startPeerConnection();
|
|
this._startPeerConnection();
|
|
@@ -1048,14 +1058,12 @@ function DataConnection(id, peer, socket, cb, options) {
|
|
var self = this;
|
|
var self = this;
|
|
if (options.sdp) {
|
|
if (options.sdp) {
|
|
this.handleSDP({ type: 'OFFER', sdp: options.sdp });
|
|
this.handleSDP({ type: 'OFFER', sdp: options.sdp });
|
|
- if (util.browserisms !== 'Firefox') {
|
|
|
|
- this._makeAnswer();
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- if (util.browserisms === 'Firefox') {
|
|
|
|
|
|
+ // Makes offer if Firefox
|
|
|
|
+ /*if (util.browserisms === 'Firefox') {
|
|
this._firefoxAdditional();
|
|
this._firefoxAdditional();
|
|
- }
|
|
|
|
|
|
+ }*/
|
|
};
|
|
};
|
|
|
|
|
|
util.inherits(DataConnection, EventEmitter);
|
|
util.inherits(DataConnection, EventEmitter);
|
|
@@ -1110,42 +1118,8 @@ DataConnection.prototype._setupIce = function() {
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
-// Awaiting update in Firefox spec ***
|
|
|
|
-/** Sets up DataChannel handlers.
|
|
|
|
-DataConnection.prototype._setupDataChannel = function() {
|
|
|
|
- var self = this;
|
|
|
|
- if (this._originator) {
|
|
|
|
-
|
|
|
|
- if (util.browserisms === 'Webkit') {
|
|
|
|
-
|
|
|
|
- // TODO: figure out the right thing to do with this.
|
|
|
|
- this._pc.onstatechange = function() {
|
|
|
|
- util.log('State Change: ', self._pc.readyState);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } else {
|
|
|
|
- this._pc.onconnection = function() {
|
|
|
|
- util.log('ORIGINATOR: onconnection triggered');
|
|
|
|
-
|
|
|
|
- self._startDataChannel();
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- this._pc.onconnection = function() {
|
|
|
|
- util.log('SINK: onconnection triggered');
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
|
|
|
|
- this._pc.onclosedconnection = function() {
|
|
|
|
- // Remove socket handlers perhaps.
|
|
|
|
- self.emit('close', self._peer);
|
|
|
|
- };
|
|
|
|
-};
|
|
|
|
-*/
|
|
|
|
-
|
|
|
|
-DataConnection.prototype._firefoxPortSetup = function() {
|
|
|
|
|
|
+/*DataConnection.prototype._firefoxPortSetup = function() {
|
|
if (!DataConnection.usedPorts) {
|
|
if (!DataConnection.usedPorts) {
|
|
DataConnection.usedPorts = [];
|
|
DataConnection.usedPorts = [];
|
|
}
|
|
}
|
|
@@ -1160,7 +1134,7 @@ DataConnection.prototype._firefoxPortSetup = function() {
|
|
}
|
|
}
|
|
DataConnection.usedPorts.push(this.remotePort);
|
|
DataConnection.usedPorts.push(this.remotePort);
|
|
DataConnection.usedPorts.push(this.localPort);
|
|
DataConnection.usedPorts.push(this.localPort);
|
|
-}
|
|
|
|
|
|
+}*/
|
|
|
|
|
|
DataConnection.prototype._configureDataChannel = function() {
|
|
DataConnection.prototype._configureDataChannel = function() {
|
|
var self = this;
|
|
var self = this;
|
|
@@ -1179,17 +1153,15 @@ DataConnection.prototype._configureDataChannel = function() {
|
|
|
|
|
|
|
|
|
|
/** Decide whether to handle Firefoxisms. */
|
|
/** Decide whether to handle Firefoxisms. */
|
|
-DataConnection.prototype._firefoxAdditional = function() {
|
|
|
|
|
|
+/*DataConnection.prototype._firefoxAdditional = function() {
|
|
var self = this;
|
|
var self = this;
|
|
getUserMedia({ audio: true, fake: true }, function(s) {
|
|
getUserMedia({ audio: true, fake: true }, function(s) {
|
|
self._pc.addStream(s);
|
|
self._pc.addStream(s);
|
|
if (self._originator) {
|
|
if (self._originator) {
|
|
self._makeOffer();
|
|
self._makeOffer();
|
|
- } else {
|
|
|
|
- self._makeAnswer();
|
|
|
|
}
|
|
}
|
|
}, function(err) { util.log('Could not getUserMedia'); });
|
|
}, function(err) { util.log('Could not getUserMedia'); });
|
|
-}
|
|
|
|
|
|
+};*/
|
|
|
|
|
|
DataConnection.prototype._makeOffer = function() {
|
|
DataConnection.prototype._makeOffer = function() {
|
|
var self = this;
|
|
var self = this;
|
|
@@ -1237,14 +1209,14 @@ DataConnection.prototype._makeAnswer = function() {
|
|
|
|
|
|
|
|
|
|
DataConnection.prototype._cleanup = function() {
|
|
DataConnection.prototype._cleanup = function() {
|
|
- if (!!this._pc && this._pc.readyState != 'closed') {
|
|
|
|
- this._pc.close();
|
|
|
|
- this._pc = null;
|
|
|
|
- }
|
|
|
|
if (!!this._dc && this._dc.readyState != 'closed') {
|
|
if (!!this._dc && this._dc.readyState != 'closed') {
|
|
this._dc.close();
|
|
this._dc.close();
|
|
this._dc = null;
|
|
this._dc = null;
|
|
}
|
|
}
|
|
|
|
+ if (!!this._pc && this._pc.readyState != 'closed') {
|
|
|
|
+ this._pc.close();
|
|
|
|
+ this._pc = null;
|
|
|
|
+ }
|
|
this.emit('close', this._peer);
|
|
this.emit('close', this._peer);
|
|
};
|
|
};
|
|
|
|
|
|
@@ -1321,9 +1293,11 @@ DataConnection.prototype.handleSDP = function(message) {
|
|
remote: self.localPort,
|
|
remote: self.localPort,
|
|
local: self.remotePort
|
|
local: self.remotePort
|
|
});
|
|
});
|
|
|
|
+ } else if (message.type === 'OFFER') {
|
|
|
|
+ self._makeAnswer();
|
|
}
|
|
}
|
|
}, function(err) {
|
|
}, function(err) {
|
|
- this._cb('Failed to setRemoteDescription');
|
|
|
|
|
|
+ self._cb('Failed to setRemoteDescription');
|
|
util.log('Failed to setRemoteDescription, ', err);
|
|
util.log('Failed to setRemoteDescription, ', err);
|
|
});
|
|
});
|
|
};
|
|
};
|
|
@@ -1341,6 +1315,7 @@ DataConnection.prototype.handleLeave = function() {
|
|
this._cleanup();
|
|
this._cleanup();
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+/*
|
|
DataConnection.prototype.handlePort = function(message) {
|
|
DataConnection.prototype.handlePort = function(message) {
|
|
if (!DataConnection.usedPorts) {
|
|
if (!DataConnection.usedPorts) {
|
|
DataConnection.usedPorts = [];
|
|
DataConnection.usedPorts = [];
|
|
@@ -1349,7 +1324,7 @@ DataConnection.prototype.handlePort = function(message) {
|
|
DataConnection.usedPorts.push(message.remote);
|
|
DataConnection.usedPorts.push(message.remote);
|
|
this._pc.connectDataConnection(message.local, message.remote);
|
|
this._pc.connectDataConnection(message.local, message.remote);
|
|
};
|
|
};
|
|
-
|
|
|
|
|
|
+*/
|
|
|
|
|
|
/**
|
|
/**
|
|
* An abstraction on top of WebSockets and XHR streaming to provide fastest
|
|
* An abstraction on top of WebSockets and XHR streaming to provide fastest
|
|
@@ -1376,8 +1351,9 @@ Socket.prototype._checkIn = function() {
|
|
var http = new XMLHttpRequest();
|
|
var http = new XMLHttpRequest();
|
|
var url = this._httpUrl + '/id';
|
|
var url = this._httpUrl + '/id';
|
|
// Set API key if necessary.
|
|
// Set API key if necessary.
|
|
- if (!!this._key)
|
|
|
|
|
|
+ if (!!this._key) {
|
|
url += '/' + this._key;
|
|
url += '/' + this._key;
|
|
|
|
+ }
|
|
|
|
|
|
// If there's no ID we need to wait for one before trying to init socket.
|
|
// If there's no ID we need to wait for one before trying to init socket.
|
|
http.open('get', url, true);
|
|
http.open('get', url, true);
|
|
@@ -1410,14 +1386,16 @@ Socket.prototype._checkIn = function() {
|
|
|
|
|
|
/** Start up websocket communications. */
|
|
/** Start up websocket communications. */
|
|
Socket.prototype._startWebSocket = function() {
|
|
Socket.prototype._startWebSocket = function() {
|
|
- if (!!this._socket)
|
|
|
|
|
|
+ if (!!this._socket) {
|
|
return;
|
|
return;
|
|
|
|
+ }
|
|
|
|
|
|
var wsurl = 'ws://' + this._server + '/ws';
|
|
var wsurl = 'ws://' + this._server + '/ws';
|
|
if (!!this._id) {
|
|
if (!!this._id) {
|
|
wsurl += '?id=' + this._id;
|
|
wsurl += '?id=' + this._id;
|
|
- if (!!this._key)
|
|
|
|
|
|
+ if (!!this._key) {
|
|
wsurl += '&key=' + this._key;
|
|
wsurl += '&key=' + this._key;
|
|
|
|
+ }
|
|
} else if (!!this._key) {
|
|
} else if (!!this._key) {
|
|
wsurl += '?key=' + this._key;
|
|
wsurl += '?key=' + this._key;
|
|
}
|
|
}
|
|
@@ -1428,7 +1406,7 @@ Socket.prototype._startWebSocket = function() {
|
|
try {
|
|
try {
|
|
self.emit('message', JSON.parse(event.data));
|
|
self.emit('message', JSON.parse(event.data));
|
|
} catch(e) {
|
|
} catch(e) {
|
|
- util.log('Invalid server message');
|
|
|
|
|
|
+ util.log('Invalid server message', event.data);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
@@ -1436,8 +1414,9 @@ Socket.prototype._startWebSocket = function() {
|
|
// socket is open.
|
|
// socket is open.
|
|
this._socket.onopen = function() {
|
|
this._socket.onopen = function() {
|
|
util.log('Socket open');
|
|
util.log('Socket open');
|
|
- if (self._id)
|
|
|
|
|
|
+ if (self._id) {
|
|
self.emit('open');
|
|
self.emit('open');
|
|
|
|
+ }
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
@@ -1450,9 +1429,9 @@ Socket.prototype._startXhrStream = function() {
|
|
var http = new XMLHttpRequest();
|
|
var http = new XMLHttpRequest();
|
|
var url = this._httpUrl + '/id';
|
|
var url = this._httpUrl + '/id';
|
|
// Set API key if necessary.
|
|
// Set API key if necessary.
|
|
- if (!!this._key)
|
|
|
|
|
|
+ if (!!this._key) {
|
|
url += '/' + this._key;
|
|
url += '/' + this._key;
|
|
-
|
|
|
|
|
|
+ }
|
|
http.open('post', url, true);
|
|
http.open('post', url, true);
|
|
http.setRequestHeader('Content-Type', 'application/json');
|
|
http.setRequestHeader('Content-Type', 'application/json');
|
|
http.onreadystatechange = function() {
|
|
http.onreadystatechange = function() {
|
|
@@ -1474,19 +1453,21 @@ Socket.prototype._handleStream = function(http, pad) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- if (this._index === undefined)
|
|
|
|
|
|
+ if (this._index === undefined) {
|
|
this._index = pad ? 2 : 1;
|
|
this._index = pad ? 2 : 1;
|
|
-
|
|
|
|
- if (http.responseText === null)
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (http.responseText === null) {
|
|
return;
|
|
return;
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
var message = http.responseText.split('\n')[this._index];
|
|
var message = http.responseText.split('\n')[this._index];
|
|
if (!!message && http.readyState == 3) {
|
|
if (!!message && http.readyState == 3) {
|
|
this._index += 1;
|
|
this._index += 1;
|
|
try {
|
|
try {
|
|
this._handleHTTPErrors(JSON.parse(message));
|
|
this._handleHTTPErrors(JSON.parse(message));
|
|
} catch(e) {
|
|
} catch(e) {
|
|
- util.log('Invalid server message');
|
|
|
|
|
|
+ util.log('Invalid server message', message);
|
|
}
|
|
}
|
|
} else if (http.readyState == 4) {
|
|
} else if (http.readyState == 4) {
|
|
this._index = 1;
|
|
this._index = 1;
|
|
@@ -1499,18 +1480,19 @@ Socket.prototype._handleHTTPErrors = function(message) {
|
|
// XHR stream closed by timeout.
|
|
// XHR stream closed by timeout.
|
|
case 'HTTP-END':
|
|
case 'HTTP-END':
|
|
util.log('XHR stream timed out.');
|
|
util.log('XHR stream timed out.');
|
|
- if (!!this._socket && this._socket.readyState != 1)
|
|
|
|
|
|
+ if (!!this._socket && this._socket.readyState != 1) {
|
|
this._startXhrStream();
|
|
this._startXhrStream();
|
|
|
|
+ }
|
|
break;
|
|
break;
|
|
// XHR stream closed by socket connect.
|
|
// XHR stream closed by socket connect.
|
|
case 'HTTP-SOCKET':
|
|
case 'HTTP-SOCKET':
|
|
- util.log('XHR stream closed, WebSocket connected.');
|
|
|
|
- break;
|
|
|
|
|
|
+ util.log('XHR stream closed, WebSocket connected.');
|
|
|
|
+ break;
|
|
case 'HTTP-ERROR':
|
|
case 'HTTP-ERROR':
|
|
- this.emit('error', 'Something went wrong.');
|
|
|
|
- break;
|
|
|
|
|
|
+ this.emit('error', 'Something went wrong.');
|
|
|
|
+ break;
|
|
default:
|
|
default:
|
|
- this.emit('message', message);
|
|
|
|
|
|
+ this.emit('message', message);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
@@ -1520,8 +1502,9 @@ Socket.prototype._handleHTTPErrors = function(message) {
|
|
Socket.prototype.send = function(data) {
|
|
Socket.prototype.send = function(data) {
|
|
var type = data.type;
|
|
var type = data.type;
|
|
message = JSON.stringify(data);
|
|
message = JSON.stringify(data);
|
|
- if (!type)
|
|
|
|
|
|
+ if (!type) {
|
|
this.emit('error', 'Invalid message');
|
|
this.emit('error', 'Invalid message');
|
|
|
|
+ }
|
|
|
|
|
|
if (!!this._socket && this._socket.readyState == 1) {
|
|
if (!!this._socket && this._socket.readyState == 1) {
|
|
this._socket.send(message);
|
|
this._socket.send(message);
|
|
@@ -1530,9 +1513,10 @@ Socket.prototype.send = function(data) {
|
|
var http = new XMLHttpRequest();
|
|
var http = new XMLHttpRequest();
|
|
var url = this._httpUrl + '/' + type.toLowerCase();
|
|
var url = this._httpUrl + '/' + type.toLowerCase();
|
|
// Set API key if necessary.
|
|
// Set API key if necessary.
|
|
- if (!!this._key)
|
|
|
|
|
|
+ if (!!this._key) {
|
|
url += '/' + this._key;
|
|
url += '/' + this._key;
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
http.open('post', url, true);
|
|
http.open('post', url, true);
|
|
http.setRequestHeader('Content-Type', 'application/json');
|
|
http.setRequestHeader('Content-Type', 'application/json');
|
|
http.onload = function() {
|
|
http.onload = function() {
|
|
@@ -1546,8 +1530,9 @@ Socket.prototype.send = function(data) {
|
|
};
|
|
};
|
|
|
|
|
|
Socket.prototype.close = function() {
|
|
Socket.prototype.close = function() {
|
|
- if (!!this._socket && this._socket.readyState == 1)
|
|
|
|
|
|
+ if (!!this._socket && this._socket.readyState == 1) {
|
|
this._socket.close();
|
|
this._socket.close();
|
|
|
|
+ }
|
|
};
|
|
};
|
|
|
|
|
|
Socket.prototype.start = function() {
|
|
Socket.prototype.start = function() {
|