dataconnection.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. this.metadata = options.metadata;
  15. this.serialization = options.serialization;
  16. this.peer = peer;
  17. this._isReliable = 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. util.log('Configuring DataChannel with peer ' + this.peer);
  26. var self = this;
  27. if (util.browserisms !== 'Webkit') {
  28. this._dc.binaryType = 'arraybuffer';
  29. }
  30. this._dc.onopen = function() {
  31. util.log('Data channel connection success');
  32. self.open = true;
  33. self.emit('open');
  34. };
  35. // Reliable.
  36. if (this._isReliable) {
  37. this._reliable = new Reliable(this._dc, util.debug);
  38. }
  39. if (this._reliable) {
  40. this._reliable.onmessage = function(msg) {
  41. self.emit('data', msg);
  42. };
  43. } else {
  44. this._dc.onmessage = function(e) {
  45. self._handleDataMessage(e);
  46. };
  47. }
  48. this._dc.onclose = function(e) {
  49. util.log('DataChannel closed.');
  50. self.close();
  51. };
  52. };
  53. DataConnection.prototype._cleanup = function() {
  54. if (!!this._dc && this._dc.readyState !== 'closed') {
  55. this._dc.close();
  56. this._dc = null;
  57. }
  58. this.open = false;
  59. this.emit('close');
  60. };
  61. // Handles a DataChannel message.
  62. DataConnection.prototype._handleDataMessage = function(e) {
  63. var self = this;
  64. var data = e.data;
  65. var datatype = data.constructor;
  66. if (this.serialization === 'binary' || this.serialization === 'binary-utf8') {
  67. if (datatype === Blob) {
  68. util.blobToArrayBuffer(data, function(ab) {
  69. data = util.unpack(ab);
  70. self.emit('data', data);
  71. });
  72. return;
  73. } else if (datatype === ArrayBuffer) {
  74. data = util.unpack(data);
  75. } else if (datatype === String) {
  76. var ab = util.binaryStringToArrayBuffer(data);
  77. data = util.unpack(ab);
  78. }
  79. } else if (this.serialization === 'json') {
  80. data = JSON.parse(data);
  81. }
  82. this.emit('data', data);
  83. };
  84. DataConnection.prototype.addDC = function(dc) {
  85. this._dc = dc;
  86. this._configureDataChannel();
  87. };
  88. /**
  89. * Exposed functionality for users.
  90. */
  91. /** Allows user to close connection. */
  92. DataConnection.prototype.close = function() {
  93. if (!this.open) {
  94. return;
  95. }
  96. this._cleanup();
  97. };
  98. /** Allows user to send data. */
  99. DataConnection.prototype.send = function(data) {
  100. if (!this.open) {
  101. this.emit('error', new Error('Connection no longer open.'));
  102. }
  103. if (this._reliable) {
  104. // Note: reliable sending will make it so that you cannot customize
  105. // serialization.
  106. this._reliable.send(data);
  107. return;
  108. }
  109. var self = this;
  110. if (this.serialization === 'none') {
  111. this._dc.send(data);
  112. } else if (this.serialization === 'json') {
  113. this._dc.send(JSON.stringify(data));
  114. } else {
  115. var utf8 = (this.serialization === 'binary-utf8');
  116. var blob = util.pack(data, utf8);
  117. // DataChannel currently only supports strings.
  118. if (util.browserisms === 'Webkit') {
  119. util.blobToBinaryString(blob, function(str){
  120. self._dc.send(str);
  121. });
  122. } else {
  123. this._dc.send(blob);
  124. }
  125. }
  126. };