dataconnection.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Wraps a DataChannel between two Peers.
  3. */
  4. function DataConnection(peer, dc, options) {
  5. if (!(this instanceof DataConnection)) return new DataConnection(peer, dc, options);
  6. EventEmitter.call(this);
  7. options = util.extend({
  8. serialization: 'binary'
  9. }, options);
  10. // Connection is not open yet.
  11. this.open = false;
  12. this.label = options.label;
  13. this.metadata = options.metadata;
  14. this.serialization = options.serialization;
  15. this.peer = peer;
  16. this.reliable = options.reliable;
  17. this._dc = dc;
  18. if (!!this._dc) {
  19. this._configureDataChannel();
  20. }
  21. };
  22. util.inherits(DataConnection, EventEmitter);
  23. DataConnection.prototype._configureDataChannel = function() {
  24. var self = this;
  25. if (util.browserisms !== 'Webkit') {
  26. this._dc.binaryType = 'arraybuffer';
  27. }
  28. this._dc.onopen = function() {
  29. util.log('Data channel connection success');
  30. self.open = true;
  31. self.emit('open');
  32. };
  33. // Reliable.
  34. if (this.reliable && util.browserisms !== 'Firefox') {
  35. this._reliable = new Reliable(this._dc, util.debug);
  36. }
  37. if (this._reliable) {
  38. this._reliable.onmessage = function(msg) {
  39. self.emit('data', msg);
  40. };
  41. } else {
  42. this._dc.onmessage = function(e) {
  43. self._handleDataMessage(e);
  44. };
  45. }
  46. this._dc.onclose = function(e) {
  47. util.log('DataChannel closed.');
  48. self.close();
  49. };
  50. };
  51. DataConnection.prototype._cleanup = function() {
  52. if (!!this._dc && this._dc.readyState !== 'closed') {
  53. this._dc.close();
  54. this._dc = null;
  55. }
  56. this.open = false;
  57. this.emit('close');
  58. };
  59. // Handles a DataChannel message.
  60. DataConnection.prototype._handleDataMessage = function(e) {
  61. var self = this;
  62. var data = e.data;
  63. var datatype = data.constructor;
  64. if (this.serialization === 'binary' || this.serialization === 'binary-utf8') {
  65. if (datatype === Blob) {
  66. util.blobToArrayBuffer(data, function(ab) {
  67. data = util.unpack(ab);
  68. self.emit('data', data);
  69. });
  70. return;
  71. } else if (datatype === ArrayBuffer) {
  72. data = util.unpack(data);
  73. } else if (datatype === String) {
  74. var ab = util.binaryStringToArrayBuffer(data);
  75. data = util.unpack(ab);
  76. }
  77. } else if (this.serialization === 'json') {
  78. data = JSON.parse(data);
  79. }
  80. this.emit('data', data);
  81. };
  82. DataConnection.prototype.addDC = function(dc) {
  83. this._dc = dc;
  84. this._configureDataChannel();
  85. };
  86. /**
  87. * Exposed functionality for users.
  88. */
  89. /** Allows user to close connection. */
  90. DataConnection.prototype.close = function() {
  91. if (!this.open) {
  92. return;
  93. }
  94. this._cleanup();
  95. };
  96. /** Allows user to send data. */
  97. DataConnection.prototype.send = function(data) {
  98. if (!this.open) {
  99. this.emit('error', new Error('Connection no longer open.'));
  100. }
  101. if (this._reliable) {
  102. // Note: reliable sending will make it so that you cannot customize
  103. // serialization.
  104. this._reliable.send(data);
  105. return;
  106. }
  107. var self = this;
  108. if (this.serialization === 'none') {
  109. this._dc.send(data);
  110. } else if (this.serialization === 'json') {
  111. this._dc.send(JSON.stringify(data));
  112. } else {
  113. var utf8 = (this.serialization === 'binary-utf8');
  114. var blob = util.pack(data, utf8);
  115. // DataChannel currently only supports strings.
  116. if (util.browserisms === 'Webkit') {
  117. util.blobToBinaryString(blob, function(str){
  118. self._dc.send(str);
  119. });
  120. } else {
  121. this._dc.send(blob);
  122. }
  123. }
  124. };
  125. /**
  126. * Returns true if the DataConnection is open and able to send messages.
  127. */
  128. DataConnection.prototype.isOpen = function() {
  129. return this.open;
  130. };
  131. /**
  132. * Gets the metadata associated with this DataConnection.
  133. */
  134. DataConnection.prototype.getMetadata = function() {
  135. return this.metadata;
  136. };
  137. /**
  138. * Gets the label associated with this DataConnection.
  139. */
  140. DataConnection.prototype.getLabel = function() {
  141. return this.label;
  142. };
  143. /**
  144. * Gets the brokering ID of the peer that you are connected with.
  145. * Note that this ID may be out of date if the peer has disconnected from the
  146. * server, so it's not recommended that you use this ID to identify this
  147. * connection.
  148. */
  149. DataConnection.prototype.getPeer = function() {
  150. return this.peer;
  151. };