JAVASCRIPT   12

websocket.js

Guest on 3rd August 2021 11:03:12 AM

  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.  
  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.  
  20.   var catchup = function() { self._stream.removeListener('data', catchup) };
  21.   this._stream.on('data', catchup);
  22.  
  23.   this._driver.io.write(body);
  24.   API.call(this, options);
  25.  
  26.   process.nextTick(function() {
  27.     self._driver.start();
  28.   });
  29. };
  30. util.inherits(WebSocket, API);
  31.  
  32. WebSocket.isWebSocket = function(request) {
  33.   return driver.isWebSocket(request);
  34. };
  35.  
  36. WebSocket.WebSocket   = WebSocket;
  37. WebSocket.Client      = require('./websocket/client');
  38. WebSocket.EventSource = require('./eventsource');
  39.  
  40. module.exports        = WebSocket;

Raw Paste


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