|
@@ -851,7 +851,7 @@ function Peer(options) {
|
|
util.debug = options.debug;
|
|
util.debug = options.debug;
|
|
|
|
|
|
this._server = options.host + ':' + options.port;
|
|
this._server = options.host + ':' + options.port;
|
|
- this._protocol = options.protocol;
|
|
|
|
|
|
+ this._httpUrl = options.protocol + '://' + this._server;
|
|
|
|
|
|
// 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))
|
|
@@ -883,24 +883,23 @@ Peer.prototype._checkIn = function() {
|
|
var self = this;
|
|
var self = this;
|
|
if (!this._id) {
|
|
if (!this._id) {
|
|
try {
|
|
try {
|
|
- http = new XMLHttpRequest();
|
|
|
|
|
|
+ var http = new XMLHttpRequest();
|
|
// 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', this._protocol + '://' + this._server + '/id', true);
|
|
|
|
|
|
+ http.open('get', this._httpUrl + '/id', true);
|
|
http.onreadystatechange = function() {
|
|
http.onreadystatechange = function() {
|
|
- if (http.readyState > 2) {
|
|
|
|
- if (!!http.responseText) {
|
|
|
|
- try {
|
|
|
|
- var response = JSON.parse(http.responseText.split('\n').shift());
|
|
|
|
- if (!!response.id) {
|
|
|
|
- self._id = response.id;
|
|
|
|
- self.emit('ready', self._id);
|
|
|
|
- }
|
|
|
|
- } catch (e) {
|
|
|
|
- // Ignore
|
|
|
|
|
|
+ if (!self._id && http.readyState > 2 && !!http.responseText) {
|
|
|
|
+ try {
|
|
|
|
+ var response = JSON.parse(http.responseText.split('\n').shift());
|
|
|
|
+ if (!!response.id) {
|
|
|
|
+ self._id = response.id;
|
|
|
|
+ self._socketInit();
|
|
|
|
+ self.emit('ready', self._id);
|
|
|
|
+ self._processQueue();
|
|
}
|
|
}
|
|
|
|
+ } catch (e) {
|
|
|
|
+ self._socketInit();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- self._socketInit();
|
|
|
|
self._handleStream(http, true);
|
|
self._handleStream(http, true);
|
|
};
|
|
};
|
|
http.send(null);
|
|
http.send(null);
|
|
@@ -909,6 +908,7 @@ Peer.prototype._checkIn = function() {
|
|
this._socketInit();
|
|
this._socketInit();
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
|
|
+ console.log('no id');
|
|
this._socketInit();
|
|
this._socketInit();
|
|
this._startXhrStream();
|
|
this._startXhrStream();
|
|
}
|
|
}
|
|
@@ -920,7 +920,7 @@ Peer.prototype._startXhrStream = function() {
|
|
try {
|
|
try {
|
|
var http = new XMLHttpRequest();
|
|
var http = new XMLHttpRequest();
|
|
var self = this;
|
|
var self = this;
|
|
- http.open('post', this._protocol + '://' + this._server + '/id', true);
|
|
|
|
|
|
+ http.open('post', this._httpUrl + '/id', true);
|
|
http.onreadystatechange = function() {
|
|
http.onreadystatechange = function() {
|
|
self._handleStream(http);
|
|
self._handleStream(http);
|
|
};
|
|
};
|
|
@@ -960,6 +960,7 @@ Peer.prototype._handleStream = function(http, pad) {
|
|
|
|
|
|
/** Start up websocket communications. */
|
|
/** Start up websocket communications. */
|
|
Peer.prototype._socketInit = function() {
|
|
Peer.prototype._socketInit = function() {
|
|
|
|
+ console.log('socketInit');
|
|
if (!!this._socket)
|
|
if (!!this._socket)
|
|
return;
|
|
return;
|
|
|
|
|
|
@@ -984,7 +985,7 @@ Peer.prototype._socketInit = function() {
|
|
metadata: message.metadata,
|
|
metadata: message.metadata,
|
|
sdp: message.sdp
|
|
sdp: message.sdp
|
|
};
|
|
};
|
|
- var connection = new DataConnection(self._id, peer, self._socket, function(err, connection) {
|
|
|
|
|
|
+ var connection = new DataConnection(self._id, peer, self._socket, self._httpUrl, function(err, connection) {
|
|
if (!err) {
|
|
if (!err) {
|
|
self.emit('connection', connection, message.metadata);
|
|
self.emit('connection', connection, message.metadata);
|
|
}
|
|
}
|
|
@@ -1016,6 +1017,11 @@ Peer.prototype._socketInit = function() {
|
|
this._socket.onopen = function() {
|
|
this._socket.onopen = function() {
|
|
util.log('Socket open');
|
|
util.log('Socket open');
|
|
self._socketOpen = true;
|
|
self._socketOpen = true;
|
|
|
|
+ for (var connection in self._connections) {
|
|
|
|
+ if (self._connections.hasOwnProperty(connection)) {
|
|
|
|
+ self._connections.connection.setSocketOpen();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
if (self._id)
|
|
if (self._id)
|
|
self._processQueue();
|
|
self._processQueue();
|
|
};
|
|
};
|
|
@@ -1051,14 +1057,14 @@ Peer.prototype.connect = function(peer, metadata, cb) {
|
|
metadata: metadata
|
|
metadata: metadata
|
|
};
|
|
};
|
|
|
|
|
|
- var connection = new DataConnection(this._id, peer, this._socket, cb, options);
|
|
|
|
|
|
+ var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, cb, options);
|
|
this.connections[peer] = connection;
|
|
this.connections[peer] = connection;
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.Peer = Peer;
|
|
exports.Peer = Peer;
|
|
|
|
|
|
-function DataConnection(id, peer, socket, cb, options) {
|
|
|
|
|
|
+function DataConnection(id, peer, socket, httpUrl, cb, options) {
|
|
if (!(this instanceof DataConnection)) return new DataConnection(options);
|
|
if (!(this instanceof DataConnection)) return new DataConnection(options);
|
|
EventEmitter.call(this);
|
|
EventEmitter.call(this);
|
|
|
|
|
|
@@ -1068,12 +1074,11 @@ function DataConnection(id, peer, socket, cb, options) {
|
|
}, options);
|
|
}, options);
|
|
this.options = options;
|
|
this.options = options;
|
|
|
|
|
|
- // Is this the originator?
|
|
|
|
-
|
|
|
|
this._id = id;
|
|
this._id = id;
|
|
this._peer = peer;
|
|
this._peer = peer;
|
|
this._originator = (options.sdp === undefined);
|
|
this._originator = (options.sdp === undefined);
|
|
this._cb = cb;
|
|
this._cb = cb;
|
|
|
|
+ this._httpUrl = httpUrl;
|
|
|
|
|
|
this.metadata = options.metadata;
|
|
this.metadata = options.metadata;
|
|
|
|
|
|
@@ -1141,6 +1146,13 @@ DataConnection.prototype._setupDataChannel = function() {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+
|
|
|
|
+/** Exposed functions for Peer. */
|
|
|
|
+
|
|
|
|
+DataConnection.prototype.setSocketOpen = function() {
|
|
|
|
+ this._socketOpen = true;
|
|
|
|
+};
|
|
|
|
+
|
|
DataConnection.prototype.handleSDP = function(message) {
|
|
DataConnection.prototype.handleSDP = function(message) {
|
|
var sdp = message.sdp;
|
|
var sdp = message.sdp;
|
|
if (util.browserisms != 'Firefox') {
|
|
if (util.browserisms != 'Firefox') {
|
|
@@ -1152,7 +1164,7 @@ DataConnection.prototype.handleSDP = function(message) {
|
|
// Firefoxism
|
|
// Firefoxism
|
|
if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
|
|
if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
|
|
self._pc.connectDataConnection(self.localPort, self.remotePort);
|
|
self._pc.connectDataConnection(self.localPort, self.remotePort);
|
|
- self._socket.send(JSON.stringify({
|
|
|
|
|
|
+ self._handleBroker('port', JSON.stringify({
|
|
type: 'PORT',
|
|
type: 'PORT',
|
|
dst: self._peer,
|
|
dst: self._peer,
|
|
src: self._id,
|
|
src: self._id,
|
|
@@ -1202,7 +1214,7 @@ DataConnection.prototype._setupIce = function() {
|
|
this._pc.onicecandidate = function(evt) {
|
|
this._pc.onicecandidate = function(evt) {
|
|
if (evt.candidate) {
|
|
if (evt.candidate) {
|
|
util.log('Received ICE candidates');
|
|
util.log('Received ICE candidates');
|
|
- self._socket.send(JSON.stringify({
|
|
|
|
|
|
+ self._handleBroker('ice', JSON.stringify({
|
|
type: 'CANDIDATE',
|
|
type: 'CANDIDATE',
|
|
candidate: evt.candidate,
|
|
candidate: evt.candidate,
|
|
dst: self._peer,
|
|
dst: self._peer,
|
|
@@ -1212,6 +1224,23 @@ DataConnection.prototype._setupIce = function() {
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+DataConnection.prototype._handleBroker = function(type, data) {
|
|
|
|
+ console.log(type);
|
|
|
|
+ if (this._socketOpen) {
|
|
|
|
+ this._socket.send(data);
|
|
|
|
+ } else {
|
|
|
|
+ console.log('http');
|
|
|
|
+ var http = new XMLHttpRequest();
|
|
|
|
+ console.log(this._httpUrl);
|
|
|
|
+ http.open('post', this._httpUrl + '/' + type, true);
|
|
|
|
+ http.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
+ http.onload = function() {
|
|
|
|
+ console.log(http.responseText);
|
|
|
|
+ }
|
|
|
|
+ http.send(data);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
|
|
// Awaiting update in Firefox spec ***
|
|
// Awaiting update in Firefox spec ***
|
|
/** Sets up DataChannel handlers.
|
|
/** Sets up DataChannel handlers.
|
|
@@ -1300,7 +1329,7 @@ DataConnection.prototype._makeOffer = function() {
|
|
util.log('Created offer');
|
|
util.log('Created offer');
|
|
self._pc.setLocalDescription(offer, function() {
|
|
self._pc.setLocalDescription(offer, function() {
|
|
util.log('Set localDescription to offer');
|
|
util.log('Set localDescription to offer');
|
|
- self._socket.send(JSON.stringify({
|
|
|
|
|
|
+ self._handleBroker('offer', JSON.stringify({
|
|
type: 'OFFER',
|
|
type: 'OFFER',
|
|
sdp: offer,
|
|
sdp: offer,
|
|
dst: self._peer,
|
|
dst: self._peer,
|
|
@@ -1321,7 +1350,7 @@ DataConnection.prototype._makeAnswer = function() {
|
|
util.log('Created answer');
|
|
util.log('Created answer');
|
|
self._pc.setLocalDescription(answer, function() {
|
|
self._pc.setLocalDescription(answer, function() {
|
|
util.log('Set localDescription to answer');
|
|
util.log('Set localDescription to answer');
|
|
- self._socket.send(JSON.stringify({
|
|
|
|
|
|
+ self._handleBroker('answer', JSON.stringify({
|
|
type: 'ANSWER',
|
|
type: 'ANSWER',
|
|
src: self._id,
|
|
src: self._id,
|
|
sdp: answer,
|
|
sdp: answer,
|
|
@@ -1356,7 +1385,7 @@ DataConnection.prototype._cleanup = function() {
|
|
DataConnection.prototype.close = function() {
|
|
DataConnection.prototype.close = function() {
|
|
this._cleanup();
|
|
this._cleanup();
|
|
var self = this;
|
|
var self = this;
|
|
- this._socket.send(JSON.stringify({
|
|
|
|
|
|
+ this._handleBroker('leave', JSON.stringify({
|
|
type: 'LEAVE',
|
|
type: 'LEAVE',
|
|
dst: self._peer,
|
|
dst: self._peer,
|
|
src: self._id,
|
|
src: self._id,
|