Sha256: 4818d4a392daa8fd50387f22fae63d966ab1c5695660013d64194c7cc6b9c6ba

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

/**
 *  Copyright (c) 2014, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 */

'use strict';

var http = require('http');
var WSS = require('websocket').server;

module.exports = Server;

/**
 * Starts an http server with the given options and attaches a websocket server
 * to it.
 *
 * @class Server
 * @param {object} options
 */

function Server(options) {
  this.log = options.log || function() {};
  this.httpServer = http.createServer(function(req, res) {
    res.writeHead(404);
    res.end();
  });
  this.httpServer.listen(options.port);
  this.wsServer = new WSS({
    httpServer: this.httpServer,
    autoAcceptConnections: false
  });
  this.wsServer.on('request', this.onRequest.bind(this));
  this.connections = [];
}

/**
 * Request handler.
 *
 * @param {object} req
 * @private
 */

Server.prototype.onRequest = function(req) {
  this.log('Client connected', req.socket.address());
  var ws = req.accept();
  this.connections.push(ws);
  ws.on('close', this.onClose.bind(this, ws));
};

/**
 * Websocket connection close handler.
 *
 * @param {object} ws
 * @private
 */

Server.prototype.onClose = function(ws) {
  this.log('Client disconnected', ws.remoteAddress);
  if (this.connections) {
    this.connections.splice(this.connections.indexOf(ws), 1);
  }
};

/**
 * Message handler.
 *
 * @param {object} msg
 * @public
 */

Server.prototype.broadcast = function(msg) {
  this.log('Broadcasting', msg);
  msg = JSON.stringify(msg);
  this.connections.forEach(function(ws) {
    ws.send(msg);
  });
};

/**
 * Close the server.
 *
 * @public
 */

Server.prototype.close = function() {
  this.log('Shutting down WebSocket server');
  this.connections = null;
  this.wsServer.shutDown();
  this.httpServer.close();
};

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
batman-rails-flo-0.1.0 vendor/assets/javascripts/batman/node_modules/fb-flo/lib/server.js
batman-rails-flo-0.0.5 vendor/assets/javascripts/batman/node_modules/fb-flo/lib/server.js
batman-rails-flo-0.0.4 vendor/assets/javascripts/batman_rails_flo/node_modules/fb-flo/lib/server.js
batman-rails-flo-0.0.3 vendor/assets/javascripts/batman_rails_flo/node_modules/fb-flo/lib/server.js
batman-rails-flo-0.0.2 vendor/assets/javascripts/batman_rails_flo/node_modules/fb-flo/lib/server.js
batman-rails-flo-0.0.1 vendor/assets/javascripts/batman_rails_flo/node_modules/fb-flo/lib/server.js