Sha256: 21edee4950ddf55f5c75f9bda8290bbb82d4cd7e65e0fcd4678f0d3ebec53d05

Contents?: true

Size: 1.94 KB

Versions: 88

Compression:

Stored size: 1.94 KB

Contents

/**
 * Module dependencies.
 */

var Transport = require('../transport')
  , parser = require('engine.io-parser')
  , debug = require('debug')('engine:ws')

/**
 * Export the constructor.
 */

module.exports = WebSocket;

/**
 * WebSocket transport 
 *
 * @param {http.ServerRequest}
 * @api public
 */

function WebSocket (req) {
  Transport.call(this, req);
  var self = this;
  this.socket = req.websocket;
  this.socket.on('message', this.onData.bind(this));
  this.socket.once('close', this.onClose.bind(this));
  this.socket.on('error', this.onError.bind(this));
  this.socket.on('headers', function (headers) {
    self.emit('headers', headers);
  });
  this.writable = true;
};

/**
 * Inherits from Transport.
 */

WebSocket.prototype.__proto__ = Transport.prototype;

/**
 * Transport name
 *
 * @api public
 */

WebSocket.prototype.name = 'websocket';

/**
 * Advertise upgrade support.
 *
 * @api public
 */

WebSocket.prototype.handlesUpgrades = true;

/**
 * Advertise framing support.
 *
 * @api public
 */

WebSocket.prototype.supportsFraming = true;

/**
 * Processes the incoming data.
 *
 * @param {String} encoded packet
 * @api private
 */

WebSocket.prototype.onData = function (data) {
  debug('received "%s"', data);
  Transport.prototype.onData.call(this, data);
};

/**
 * Writes a packet payload.
 *
 * @param {Array} packets
 * @api private
 */

WebSocket.prototype.send = function (packets) {
  var self = this;
  for (var i = 0, l = packets.length; i < l; i++) {
    parser.encodePacket(packets[i], this.supportsBinary, function(data) {
      debug('writing "%s"', data);
      self.writable = false;
      self.socket.send(data, function (err){
        if (err) return self.onError('write error', err.stack);
        self.writable = true;
        self.emit('drain');
      });
    });
  }
};

/**
 * Closes the transport.
 *
 * @api private
 */

WebSocket.prototype.doClose = function (fn) {
  debug('closing');
  this.socket.close();
  fn && fn();
};

Version data entries

88 entries across 88 versions & 2 rubygems

Version Path
flok-0.0.105 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.103 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.102 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
gulp_assets-1.0.0.pre.5 template/node_modules/webpack-dev-server/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
gulp_assets-1.0.0.pre.4 template/node_modules/webpack-dev-server/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
gulp_assets-1.0.0.pre.3 template/node_modules/webpack-dev-server/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.101 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.100 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.99 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.98 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.97 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.96 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.95 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.94 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.93 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.92 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.91 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.90 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.89 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js
flok-0.0.88 spec/iface/driver/assets/node_modules/socket.io/node_modules/engine.io/lib/transports/websocket.js