JAVASCRIPT   62

websocket.js

Guest on 21st August 2021 08:48:52 PM

  1. // API references:
  2. //
  3. // * https://html.spec.whatwg.org/multipage/comms.html#network
  4. // * https://dom.spec.whatwg.org/#interface-eventtarget
  5. // * https://dom.spec.whatwg.org/#interface-event
  6.  
  7. var util   = require('util'),
  8.     driver = require('websocket-driver'),
  9.     API    = require('./websocket/api');
  10.  
  11. var WebSocket = function(request, socket, body, protocols, options) {
  12.   options = options || {};
  13.  
  14.   this._stream = socket;
  15.   this._driver = driver.http(request, {maxLength: options.maxLength, protocols: protocols});
  16.  
  17.   var self = this;
  18.   if (!this._stream || !this._stream.writable) return;
  19.   if (!this._stream.readable) return this._stream.end();
  20.  
  21.   var catchup = function() { self._stream.removeListener('data', catchup) };
  22.   this._stream.on('data', catchup);
  23.  
  24.   API.call(this, options);
  25.  
  26.   process.nextTick(function() {
  27.     self._driver.start();
  28.     self._driver.io.write(body);
  29.   });
  30. };
  31. util.inherits(WebSocket, API);
  32.  
  33. WebSocket.isWebSocket = function(request) {
  34.   return driver.isWebSocket(request);
  35. };
  36.  
  37. WebSocket.validateOptions = function(options, validKeys) {
  38.   driver.validateOptions(options, validKeys);
  39. };
  40.  
  41. WebSocket.WebSocket   = WebSocket;
  42. WebSocket.Client      = require('./websocket/client');
  43. WebSocket.EventSource = require('./eventsource');
  44.  
  45. module.exports        = WebSocket;

Raw Paste


Login or Register to edit or fork this paste. It's free.