|
@@ -14,7 +14,7 @@ function Peer(id, options) {
|
|
|
id = id.toString();
|
|
|
}
|
|
|
//
|
|
|
-
|
|
|
+
|
|
|
// Configurize options
|
|
|
options = util.extend({
|
|
|
debug: 0, // 1: Errors, 2: Warnings, 3: All logs
|
|
@@ -39,7 +39,7 @@ function Peer(id, options) {
|
|
|
}
|
|
|
util.setLogLevel(options.debug);
|
|
|
//
|
|
|
-
|
|
|
+
|
|
|
// Sanity checks
|
|
|
// Ensure WebRTC supported
|
|
|
if (!util.supports.audioVideo && !util.supports.data ) {
|
|
@@ -72,7 +72,23 @@ function Peer(id, options) {
|
|
|
|
|
|
// References
|
|
|
this.connections = {}; // DataConnections for this peer.
|
|
|
- this.calls = {}; // MediaConnections for this peer
|
|
|
+ //
|
|
|
+
|
|
|
+ // Initialize the 'socket' (which is actually a mix of XHR streaming and
|
|
|
+ // websockets.)
|
|
|
+ var self = this;
|
|
|
+ this.socket = new Socket(this.options.secure, this.options.host, this.options.port, this.options.key);
|
|
|
+ this.socket.on('message', function(data) {
|
|
|
+ self._handleMessage(data);
|
|
|
+ });
|
|
|
+ this.socket.on('error', function(error) {
|
|
|
+ self._abort('socket-error', error);
|
|
|
+ });
|
|
|
+ this.socket.on('close', function() {
|
|
|
+ if (!self.disconnected) { // If we haven't explicitly disconnected, emit error.
|
|
|
+ self._abort('socket-closed', 'Underlying socket is already closed.');
|
|
|
+ }
|
|
|
+ });
|
|
|
//
|
|
|
|
|
|
// Start the connections
|
|
@@ -119,22 +135,7 @@ Peer.prototype._retrieveId = function(cb) {
|
|
|
Peer.prototype._initialize = function(id) {
|
|
|
var self = this;
|
|
|
this.id = id;
|
|
|
-
|
|
|
- // Initialize the 'socket' (which is actually a mix of XHR streaming and
|
|
|
- // websockets.
|
|
|
- this.socket = new Socket(this.options.secure, this.options.host, this.options.port, this.options.key, this.id);
|
|
|
- this.socket.on('message', function(data) {
|
|
|
- self._handleMessage(data);
|
|
|
- });
|
|
|
- this.socket.on('error', function(error) {
|
|
|
- self._abort('socket-error', error);
|
|
|
- });
|
|
|
- this.socket.on('close', function() {
|
|
|
- if (!self.disconnected) { // If we haven't explicitly disconnected, emit error.
|
|
|
- self._abort('socket-closed', 'Underlying socket is already closed.');
|
|
|
- }
|
|
|
- });
|
|
|
- this.socket.start();
|
|
|
+ this.socket.start(this.id);
|
|
|
}
|
|
|
|
|
|
/** Handles messages from the server. */
|
|
@@ -169,7 +170,7 @@ Peer.prototype._handleMessage = function(message) {
|
|
|
break;
|
|
|
case 'OFFER': // we should consider switching this to CALL/CONNECT, but this is the least breaking option.
|
|
|
var connectionId = payload.connectionId;
|
|
|
- var connection = this._getConnection(peer, connectionId);
|
|
|
+ var connection = this.getConnection(peer, connectionId);
|
|
|
|
|
|
if (connection) {
|
|
|
util.warn('Offer received for existing Connection ID:', connectionId);
|
|
@@ -202,7 +203,7 @@ Peer.prototype._handleMessage = function(message) {
|
|
|
break;
|
|
|
default:
|
|
|
var id = message.id;
|
|
|
- var connection = this._getConnection(peer, id);
|
|
|
+ var connection = this.getConnection(peer, id);
|
|
|
|
|
|
if (connection) {
|
|
|
// Pass it on.
|
|
@@ -248,7 +249,7 @@ Peer.prototype._addConnection = function(peer, connection) {
|
|
|
}
|
|
|
|
|
|
/** Retrieve a data/media connection for this peer. */
|
|
|
-Peer.prototype._getConnection = function(peer, id) {
|
|
|
+Peer.prototype.getConnection = function(peer, id) {
|
|
|
var connections = this.connections[peer];
|
|
|
if (!connections) {
|
|
|
return null;
|