web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.2 KiB

  1. 'use strict';
  2. var Stream = require('stream').Stream,
  3. util = require('util'),
  4. driver = require('websocket-driver'),
  5. EventTarget = require('./api/event_target'),
  6. Event = require('./api/event');
  7. var API = function(options) {
  8. options = options || {};
  9. driver.validateOptions(options, ['headers', 'extensions', 'maxLength', 'ping', 'proxy', 'tls', 'ca']);
  10. this.readable = this.writable = true;
  11. var headers = options.headers;
  12. if (headers) {
  13. for (var name in headers) this._driver.setHeader(name, headers[name]);
  14. }
  15. var extensions = options.extensions;
  16. if (extensions) {
  17. [].concat(extensions).forEach(this._driver.addExtension, this._driver);
  18. }
  19. this._ping = options.ping;
  20. this._pingId = 0;
  21. this.readyState = API.CONNECTING;
  22. this.bufferedAmount = 0;
  23. this.protocol = '';
  24. this.url = this._driver.url;
  25. this.version = this._driver.version;
  26. var self = this;
  27. this._driver.on('open', function(e) { self._open() });
  28. this._driver.on('message', function(e) { self._receiveMessage(e.data) });
  29. this._driver.on('close', function(e) { self._beginClose(e.reason, e.code) });
  30. this._driver.on('error', function(error) {
  31. self._emitError(error.message);
  32. });
  33. this.on('error', function() {});
  34. this._driver.messages.on('drain', function() {
  35. self.emit('drain');
  36. });
  37. if (this._ping)
  38. this._pingTimer = setInterval(function() {
  39. self._pingId += 1;
  40. self.ping(self._pingId.toString());
  41. }, this._ping * 1000);
  42. this._configureStream();
  43. if (!this._proxy) {
  44. this._stream.pipe(this._driver.io);
  45. this._driver.io.pipe(this._stream);
  46. }
  47. };
  48. util.inherits(API, Stream);
  49. API.CONNECTING = 0;
  50. API.OPEN = 1;
  51. API.CLOSING = 2;
  52. API.CLOSED = 3;
  53. API.CLOSE_TIMEOUT = 30000;
  54. var instance = {
  55. write: function(data) {
  56. return this.send(data);
  57. },
  58. end: function(data) {
  59. if (data !== undefined) this.send(data);
  60. this.close();
  61. },
  62. pause: function() {
  63. return this._driver.messages.pause();
  64. },
  65. resume: function() {
  66. return this._driver.messages.resume();
  67. },
  68. send: function(data) {
  69. if (this.readyState > API.OPEN) return false;
  70. if (!(data instanceof Buffer)) data = String(data);
  71. return this._driver.messages.write(data);
  72. },
  73. ping: function(message, callback) {
  74. if (this.readyState > API.OPEN) return false;
  75. return this._driver.ping(message, callback);
  76. },
  77. close: function(code, reason) {
  78. if (code === undefined) code = 1000;
  79. if (reason === undefined) reason = '';
  80. if (code !== 1000 && (code < 3000 || code > 4999))
  81. throw new Error("Failed to execute 'close' on WebSocket: " +
  82. "The code must be either 1000, or between 3000 and 4999. " +
  83. code + " is neither.");
  84. if (this.readyState !== API.CLOSED) this.readyState = API.CLOSING;
  85. var self = this;
  86. this._closeTimer = setTimeout(function() {
  87. self._beginClose('', 1006);
  88. }, API.CLOSE_TIMEOUT);
  89. this._driver.close(reason, code);
  90. },
  91. _configureStream: function() {
  92. var self = this;
  93. this._stream.setTimeout(0);
  94. this._stream.setNoDelay(true);
  95. ['close', 'end'].forEach(function(event) {
  96. this._stream.on(event, function() { self._finalizeClose() });
  97. }, this);
  98. this._stream.on('error', function(error) {
  99. self._emitError('Network error: ' + self.url + ': ' + error.message);
  100. self._finalizeClose();
  101. });
  102. },
  103. _open: function() {
  104. if (this.readyState !== API.CONNECTING) return;
  105. this.readyState = API.OPEN;
  106. this.protocol = this._driver.protocol || '';
  107. var event = new Event('open');
  108. event.initEvent('open', false, false);
  109. this.dispatchEvent(event);
  110. },
  111. _receiveMessage: function(data) {
  112. if (this.readyState > API.OPEN) return false;
  113. if (this.readable) this.emit('data', data);
  114. var event = new Event('message', {data: data});
  115. event.initEvent('message', false, false);
  116. this.dispatchEvent(event);
  117. },
  118. _emitError: function(message) {
  119. if (this.readyState >= API.CLOSING) return;
  120. var event = new Event('error', {message: message});
  121. event.initEvent('error', false, false);
  122. this.dispatchEvent(event);
  123. },
  124. _beginClose: function(reason, code) {
  125. if (this.readyState === API.CLOSED) return;
  126. this.readyState = API.CLOSING;
  127. this._closeParams = [reason, code];
  128. if (this._stream) {
  129. this._stream.destroy();
  130. if (!this._stream.readable) this._finalizeClose();
  131. }
  132. },
  133. _finalizeClose: function() {
  134. if (this.readyState === API.CLOSED) return;
  135. this.readyState = API.CLOSED;
  136. if (this._closeTimer) clearTimeout(this._closeTimer);
  137. if (this._pingTimer) clearInterval(this._pingTimer);
  138. if (this._stream) this._stream.end();
  139. if (this.readable) this.emit('end');
  140. this.readable = this.writable = false;
  141. var reason = this._closeParams ? this._closeParams[0] : '',
  142. code = this._closeParams ? this._closeParams[1] : 1006;
  143. var event = new Event('close', {code: code, reason: reason});
  144. event.initEvent('close', false, false);
  145. this.dispatchEvent(event);
  146. }
  147. };
  148. for (var method in instance) API.prototype[method] = instance[method];
  149. for (var key in EventTarget) API.prototype[key] = EventTarget[key];
  150. module.exports = API;