123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- /**
- * Wraps a DataChannel between two Peers.
- */
- function DataConnection(peer, options) {
- if (!(this instanceof DataConnection)) return new DataConnection(peer, options);
- EventEmitter.call(this);
- // TODO: perhaps default serialization should be binary-utf8?
- options = util.extend({
- serialization: 'binary'
- }, options);
- // Connection is not open yet.
- this.open = false;
- this.label = options.label;
- this.metadata = options.metadata;
- this.serialization = options.serialization;
- this.peer = peer;
- this.reliable = options.reliable;
- this.id = options.id; // TODO: or randomly generated ID.
- if (options.sdp) {
- // TODO: startConnection ideally calls handleSDP with the offer.
- // On this side though, do we also need to pass in this peer's ID? Probably.
- // Maybe instead of having a global negotiator, have a negotiator instance
- // per Peer?
- Negotiator.startConnection(this.peer, this.id, options.sdp, options.config);
- }
- /*this._dc = dc;
- if (this._dc) {
- this._configureDataChannel();
- }*/
- };
- util.inherits(DataConnection, EventEmitter);
- DataConnection.prototype._configureDataChannel = function() {
- var self = this;
- // TODO: util.supports.binary
- if (util.supports.binary) {
- // Webkit doesn't support binary yet
- this._dc.binaryType = 'arraybuffer';
- }
- this._dc.onopen = function() {
- util.log('Data channel connection success');
- self.open = true;
- self.emit('open');
- };
- // Use the Reliable shim for non Firefox browsers
- // TODO: util.supports.reliable
- if (!util.supports.reliable) {
- this._reliable = new Reliable(this._dc, util.debug);
- }
- if (this._reliable) {
- this._reliable.onmessage = function(msg) {
- self.emit('data', msg);
- };
- } else {
- this._dc.onmessage = function(e) {
- self._handleDataMessage(e);
- };
- }
- this._dc.onclose = function(e) {
- util.log('DataChannel closed.');
- self.close();
- };
- };
- DataConnection.prototype._cleanup = function() {
- if (this._dc && this._dc.readyState !== 'closed') {
- this._dc.close();
- this._dc = null;
- }
- this.open = false;
- this.emit('close');
- };
- // Handles a DataChannel message.
- DataConnection.prototype._handleDataMessage = function(e) {
- var self = this;
- var data = e.data;
- var datatype = data.constructor;
- if (this.serialization === 'binary' || this.serialization === 'binary-utf8') {
- if (datatype === Blob) {
- // Datatype should never be blob
- util.blobToArrayBuffer(data, function(ab) {
- data = util.unpack(ab);
- self.emit('data', data);
- });
- return;
- } else if (datatype === ArrayBuffer) {
- data = util.unpack(data);
- } else if (datatype === String) {
- // String fallback for binary data for browsers that don't support binary yet
- var ab = util.binaryStringToArrayBuffer(data);
- data = util.unpack(ab);
- }
- } else if (this.serialization === 'json') {
- data = JSON.parse(data);
- }
- this.emit('data', data);
- }
- DataConnection.prototype.addDC = function(dc) {
- this._dc = dc;
- this._configureDataChannel();
- }
- /**
- * Exposed functionality for users.
- */
- /** Allows user to close connection. */
- DataConnection.prototype.close = function() {
- if (!this.open) {
- return;
- }
- this._cleanup();
- };
- /** Allows user to send data. */
- DataConnection.prototype.send = function(data) {
- if (!this.open) {
- this.emit('error', new Error('Connection no longer open.'));
- }
- if (this._reliable) {
- // Note: reliable shim sending will make it so that you cannot customize
- // serialization.
- this._reliable.send(data);
- return;
- }
- var self = this;
- if (this.serialization === 'none') {
- this._dc.send(data);
- } else if (this.serialization === 'json') {
- this._dc.send(JSON.stringify(data));
- } else {
- var utf8 = (this.serialization === 'binary-utf8');
- var blob = util.pack(data, utf8);
- // DataChannel currently only supports strings.
- if (!util.supports.binary) {
- util.blobToBinaryString(blob, function(str){
- self._dc.send(str);
- });
- } else {
- this._dc.send(blob);
- }
- }
- };
- DataConnection.prototype.handleMessage = function(message) {
- var payload = message.payload;
- switch (message.type) {
- case 'ANSWER':
- // TODO: assert sdp exists.
- // Should we pass `this`?
- // Forward to negotiator
- Negotiator.handleSDP(this.peer, this.id, payload.sdp, message.type);
- break;
- case 'CANDIDATE':
- Negotiator.handleCandidate(this.peer, this.id, payload.candidate);
- break;
- default:
- util.warn('Unrecognized message type:', message.type, 'from peer:', this.peer);
- break;
- }
- }
|