dataconnection.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Wraps a DataChannel between two Peers.
  3. */
  4. function DataConnection(peer, provider, options) {
  5. if (!(this instanceof DataConnection)) return new DataConnection(peer, options);
  6. EventEmitter.call(this);
  7. // TODO: perhaps default serialization should be binary-utf8?
  8. options = util.extend({
  9. serialization: 'binary'
  10. }, options);
  11. // Connection is not open yet.
  12. this.open = false;
  13. this.type = 'data';
  14. this.peer = peer;
  15. this.provider = provider;
  16. if (options) {
  17. this.label = options.label;
  18. this.metadata = options.metadata;
  19. this.serialization = options.serialization;
  20. this.reliable = options.reliable;
  21. this.id = options._id;
  22. }
  23. if (!this.id) {
  24. this.id = DataConnection._idPrefix + util.randomToken();
  25. }
  26. this._pc = Negotiator.startConnection(
  27. this.peer,
  28. this.id,
  29. this.provider,
  30. (options && options.payload) ? options.payload : null
  31. );
  32. /*this._dc = dc;
  33. if (this._dc) {
  34. this._configureDataChannel();
  35. }*/
  36. }
  37. util.inherits(DataConnection, EventEmitter);
  38. DataConnection._idPrefix = 'dc_';
  39. /** Called by the Negotiator when the DataChannel is ready. */
  40. DataConnection.prototype.initialize = function(dc) {
  41. this._dc = dc;
  42. }
  43. DataConnection.prototype._configureDataChannel = function() {
  44. var self = this;
  45. // TODO: util.supports.binary
  46. if (util.supports.binary) {
  47. // Webkit doesn't support binary yet
  48. this._dc.binaryType = 'arraybuffer';
  49. }
  50. this._dc.onopen = function() {
  51. util.log('Data channel connection success');
  52. self.open = true;
  53. self.emit('open');
  54. };
  55. // Use the Reliable shim for non Firefox browsers
  56. // TODO: util.supports.reliable
  57. if (!util.supports.reliable) {
  58. this._reliable = new Reliable(this._dc, util.debug);
  59. }
  60. if (this._reliable) {
  61. this._reliable.onmessage = function(msg) {
  62. self.emit('data', msg);
  63. };
  64. } else {
  65. this._dc.onmessage = function(e) {
  66. self._handleDataMessage(e);
  67. };
  68. }
  69. this._dc.onclose = function(e) {
  70. util.log('DataChannel closed.');
  71. self.close();
  72. };
  73. };
  74. DataConnection.prototype._cleanup = function() {
  75. if (this._dc && this._dc.readyState !== 'closed') {
  76. this._dc.close();
  77. this._dc = null;
  78. }
  79. this.open = false;
  80. this.emit('close');
  81. };
  82. // Handles a DataChannel message.
  83. DataConnection.prototype._handleDataMessage = function(e) {
  84. var self = this;
  85. var data = e.data;
  86. var datatype = data.constructor;
  87. if (this.serialization === 'binary' || this.serialization === 'binary-utf8') {
  88. if (datatype === Blob) {
  89. // Datatype should never be blob
  90. util.blobToArrayBuffer(data, function(ab) {
  91. data = util.unpack(ab);
  92. self.emit('data', data);
  93. });
  94. return;
  95. } else if (datatype === ArrayBuffer) {
  96. data = util.unpack(data);
  97. } else if (datatype === String) {
  98. // String fallback for binary data for browsers that don't support binary yet
  99. var ab = util.binaryStringToArrayBuffer(data);
  100. data = util.unpack(ab);
  101. }
  102. } else if (this.serialization === 'json') {
  103. data = JSON.parse(data);
  104. }
  105. this.emit('data', data);
  106. }
  107. DataConnection.prototype.addDC = function(dc) {
  108. this._dc = dc;
  109. this._configureDataChannel();
  110. }
  111. /**
  112. * Exposed functionality for users.
  113. */
  114. /** Allows user to close connection. */
  115. DataConnection.prototype.close = function() {
  116. if (!this.open) {
  117. return;
  118. }
  119. this._cleanup();
  120. };
  121. /** Allows user to send data. */
  122. DataConnection.prototype.send = function(data) {
  123. if (!this.open) {
  124. this.emit('error', new Error('Connection no longer open.'));
  125. }
  126. if (this._reliable) {
  127. // Note: reliable shim sending will make it so that you cannot customize
  128. // serialization.
  129. this._reliable.send(data);
  130. return;
  131. }
  132. var self = this;
  133. if (this.serialization === 'none') {
  134. this._dc.send(data);
  135. } else if (this.serialization === 'json') {
  136. this._dc.send(JSON.stringify(data));
  137. } else {
  138. var utf8 = (this.serialization === 'binary-utf8');
  139. var blob = util.pack(data, utf8);
  140. // DataChannel currently only supports strings.
  141. if (!util.supports.binary) {
  142. util.blobToBinaryString(blob, function(str){
  143. self._dc.send(str);
  144. });
  145. } else {
  146. this._dc.send(blob);
  147. }
  148. }
  149. };
  150. DataConnection.prototype.handleMessage = function(message) {
  151. var payload = message.payload;
  152. switch (message.type) {
  153. case 'ANSWER':
  154. // TODO: assert sdp exists.
  155. // Should we pass `this`?
  156. // Forward to negotiator
  157. Negotiator.handleSDP(this.peer, this.id, payload.sdp, message.type);
  158. break;
  159. case 'CANDIDATE':
  160. Negotiator.handleCandidate(this.peer, this.id, payload.candidate);
  161. break;
  162. default:
  163. util.warn('Unrecognized message type:', message.type, 'from peer:', this.peer);
  164. break;
  165. }
  166. }