dataconnection.js 3.2 KB

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