|
@@ -101,7 +101,7 @@ Peer.prototype._init = function() {
|
|
|
|
|
|
Peer.prototype._handleServerJSONMessage = function(message) {
|
|
|
var peer = message.src;
|
|
|
- var connection = this.connections[peer];
|
|
|
+ var manager = this.managers[peer];
|
|
|
var payload = message.payload;
|
|
|
switch (message.type) {
|
|
|
case 'OPEN':
|
|
@@ -123,31 +123,36 @@ Peer.prototype._handleServerJSONMessage = function(message) {
|
|
|
reliable: payload.reliable,
|
|
|
config: this._options.config
|
|
|
};
|
|
|
- var connection = new DataConnection(this.id, peer, this._socket, options);
|
|
|
- this._attachConnectionListeners(connection);
|
|
|
- this.connections[peer] = connection;
|
|
|
- this.emit('connection', connection, payload.metadata);
|
|
|
+
|
|
|
+ var manager = this.managers[peer];
|
|
|
+ if (!!manager) {
|
|
|
+ manager = new ConnectionManager(this.id, peer, this._socket, options);
|
|
|
+ this._attachManagerListeners(manager);
|
|
|
+ this.managers[peer] = manager;
|
|
|
+ this.connections[peer] = {};
|
|
|
+ }
|
|
|
+ // MOVE THIS TO ONDATACHANNEL
|
|
|
+ //this.emit('connection', connection, payload.metadata);
|
|
|
break;
|
|
|
case 'EXPIRE':
|
|
|
- connection = this.connections[peer];
|
|
|
- if (connection) {
|
|
|
- connection.close();
|
|
|
- connection.emit('error', new Error('Could not connect to peer ' + connection.peer));
|
|
|
+ if (manager) {
|
|
|
+ manager.close();
|
|
|
+ manager.emit('error', new Error('Could not connect to peer ' + manager.peer));
|
|
|
}
|
|
|
break;
|
|
|
case 'ANSWER':
|
|
|
- if (connection) {
|
|
|
- connection.handleSDP(payload.sdp, message.type);
|
|
|
+ if (manager) {
|
|
|
+ manager.handleSDP(payload.sdp, message.type);
|
|
|
}
|
|
|
break;
|
|
|
case 'CANDIDATE':
|
|
|
- if (connection) {
|
|
|
- connection.handleCandidate(payload);
|
|
|
+ if (manager) {
|
|
|
+ manager.handleCandidate(payload);
|
|
|
}
|
|
|
break;
|
|
|
case 'LEAVE':
|
|
|
- if (connection) {
|
|
|
- connection.handleLeave();
|
|
|
+ if (manager) {
|
|
|
+ manager.handleLeave();
|
|
|
}
|
|
|
break;
|
|
|
case 'INVALID-KEY':
|
|
@@ -182,10 +187,10 @@ Peer.prototype._abort = function(type, message) {
|
|
|
|
|
|
Peer.prototype._cleanup = function() {
|
|
|
var self = this;
|
|
|
- if (!!this.connections) {
|
|
|
- var peers = Object.keys(this.connections);
|
|
|
+ if (!!this.managers) {
|
|
|
+ var peers = Object.keys(this.managers);
|
|
|
for (var i = 0, ii = peers.length; i < ii; i++) {
|
|
|
- this.connections[peers[i]].close();
|
|
|
+ this.managers[peers[i]].close();
|
|
|
}
|
|
|
util.setZeroTimeout(function(){
|
|
|
self._socket.close();
|
|
@@ -194,16 +199,6 @@ Peer.prototype._cleanup = function() {
|
|
|
this.emit('close');
|
|
|
};
|
|
|
|
|
|
-/** Listeners for DataConnection events. */
|
|
|
-Peer.prototype._attachConnectionListeners = function(connection) {
|
|
|
- var self = this;
|
|
|
- connection.on('close', function(peer) {
|
|
|
- if (self.connections[peer]) {
|
|
|
- delete self.connections[peer];
|
|
|
- }
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
|
|
|
|
|
|
/** Exposed connect function for users. Will try to connect later if user
|
|
@@ -226,11 +221,11 @@ Peer.prototype.connect = function(peer, options) {
|
|
|
manager = new ConnectionManager(this.id, peer, this._socket, options);
|
|
|
this._attachManagerListeners(manager);
|
|
|
this.managers[peer] = manager;
|
|
|
- this.connections[peer] = [];
|
|
|
+ this.connections[peer] = {};
|
|
|
}
|
|
|
|
|
|
var connection = manager.connect(options.label);
|
|
|
- this.connections[peer].push(connection);
|
|
|
+ this.connections[peer][options.label] = connection;
|
|
|
|
|
|
if (!this.id) {
|
|
|
this._queued.push(manager);
|