dataconnection.js 3.3 KB

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