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.

45 lines
1.3 KiB

4 years ago
  1. // API references:
  2. //
  3. // * http://dev.w3.org/html5/websockets/
  4. // * http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-eventtarget
  5. // * http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-event
  6. var util = require('util'),
  7. driver = require('websocket-driver'),
  8. API = require('./websocket/api');
  9. var WebSocket = function(request, socket, body, protocols, options) {
  10. options = options || {};
  11. this._stream = socket;
  12. this._driver = driver.http(request, {maxLength: options.maxLength, protocols: protocols});
  13. var self = this;
  14. if (!this._stream || !this._stream.writable) return;
  15. if (!this._stream.readable) return this._stream.end();
  16. var catchup = function() { self._stream.removeListener('data', catchup) };
  17. this._stream.on('data', catchup);
  18. API.call(this, options);
  19. process.nextTick(function() {
  20. self._driver.start();
  21. self._driver.io.write(body);
  22. });
  23. };
  24. util.inherits(WebSocket, API);
  25. WebSocket.isWebSocket = function(request) {
  26. return driver.isWebSocket(request);
  27. };
  28. WebSocket.validateOptions = function(options, validKeys) {
  29. driver.validateOptions(options, validKeys);
  30. };
  31. WebSocket.WebSocket = WebSocket;
  32. WebSocket.Client = require('./websocket/client');
  33. WebSocket.EventSource = require('./eventsource');
  34. module.exports = WebSocket;