dataconnection.js 4.5 KB

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