dataconnection.js 4.3 KB

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