dataconnection.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. reliable: false,
  9. serialization: 'binary'
  10. }, options);
  11. // Connection is not open yet.
  12. this.open = false;
  13. this.label = options.label;
  14. // Firefox is not this finely configurable.
  15. this.metadata = options.metadata;
  16. this.serialization = util.browserisms !== 'Firefox' ? options.serialization : 'binary';
  17. this._isReliable = util.browserisms !== 'Firefox' ? options.reliable : true;
  18. this.peer = peer;
  19. this._dc = dc;
  20. if (!!this._dc) {
  21. this._configureDataChannel();
  22. }
  23. };
  24. util.inherits(DataConnection, EventEmitter);
  25. DataConnection.prototype._configureDataChannel = function() {
  26. util.log('Configuring DataChannel with peer ' + this.peer);
  27. var self = this;
  28. if (util.browserisms !== 'Webkit') {
  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. // Reliable.
  37. if (this._isReliable) {
  38. this._reliable = new Reliable(this._dc, util.debug);
  39. }
  40. if (this._reliable) {
  41. this._reliable.onmessage = function(msg) {
  42. self.emit('data', msg);
  43. };
  44. } else {
  45. this._dc.onmessage = function(e) {
  46. self._handleDataMessage(e);
  47. };
  48. }
  49. this._dc.onclose = function(e) {
  50. util.log('DataChannel closed.');
  51. self.close();
  52. };
  53. };
  54. DataConnection.prototype._cleanup = function() {
  55. if (!!this._dc && this._dc.readyState !== 'closed') {
  56. this._dc.close();
  57. this._dc = null;
  58. }
  59. this.open = false;
  60. this.emit('close');
  61. };
  62. // Handles a DataChannel message.
  63. DataConnection.prototype._handleDataMessage = function(e) {
  64. var self = this;
  65. var data = e.data;
  66. var datatype = data.constructor;
  67. if (this.serialization === 'binary' || this.serialization === 'binary-utf8') {
  68. if (datatype === Blob) {
  69. util.blobToArrayBuffer(data, function(ab) {
  70. data = util.unpack(ab);
  71. self.emit('data', data);
  72. });
  73. return;
  74. } else if (datatype === ArrayBuffer) {
  75. data = util.unpack(data);
  76. } else if (datatype === String) {
  77. var ab = util.binaryStringToArrayBuffer(data);
  78. data = util.unpack(ab);
  79. }
  80. } else if (this.serialization === 'json') {
  81. data = JSON.parse(data);
  82. }
  83. this.emit('data', data);
  84. };
  85. DataConnection.prototype.addDC = function(dc) {
  86. this._dc = dc;
  87. this._configureDataChannel();
  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._cleanup();
  98. };
  99. /** Allows user to send data. */
  100. DataConnection.prototype.send = function(data) {
  101. if (!this.open) {
  102. this.emit('error', new Error('Connection no longer open.'));
  103. }
  104. if (this._reliable) {
  105. // Note: reliable sending will make it so that you cannot customize
  106. // serialization.
  107. this._reliable.send(data);
  108. return;
  109. }
  110. var self = this;
  111. if (this.serialization === 'none') {
  112. this._dc.send(data);
  113. } else if (this.serialization === 'json') {
  114. this._dc.send(JSON.stringify(data));
  115. } else {
  116. var utf8 = (this.serialization === 'binary-utf8');
  117. var blob = util.pack(data, utf8);
  118. // DataChannel currently only supports strings.
  119. if (util.browserisms === 'Webkit') {
  120. util.blobToBinaryString(blob, function(str){
  121. self._dc.send(str);
  122. });
  123. } else {
  124. this._dc.send(blob);
  125. }
  126. }
  127. };