lib/camp.js
function wsUnit (server) {
  var chanPool = server.wsChannels = {};
  // Main WebSocket API:
  // ws(channel :: String, conListener :: function(socket))
  server.ws = function ws (channel, conListener) {
    if (channel[0] !== '/') {
      channel = '/$websocket:' + channel;  // Deprecated API.
    }
    if (chanPool[channel] !== undefined) {
      chanPool[channel].close();
    }
    chanPool[channel] = new WebSocket.Server({
      server: server,
      path: channel,
    });
    chanPool[channel].on('connection', conListener);
    return chanPool[channel];
  };
  // WebSocket broadcast API.
  // webBroadcast(channel :: String, recvListener :: function(data, end))
  server.wsBroadcast = function wsBroadcast (channel, recvListener) {
    if (channel[0] === '/') {
      return server.ws(channel, function (socket) {
        socket.on('message', function wsBroadcastRecv (data, flags) {
          recvListener({data: data, flags: flags}, {
            send: function wsBroadcastSend (dataBack) {
              chanPool[channel].clients.forEach(function (s) {
                s.send(dataBack);
              });
            },
          });
        });
      });
    } else { // Deprecated API
      return server.ws(channel, function (socket) {
        socket.on('message', function wsBroadcastRecv (data, flags) {
          recvListener(data, function wsBroadcastSend (dataBack) {
            chanPool[channel].clients.forEach(function (s) { s.send(dataBack); });
          });
        });
      });
    }
  };

  return function wsLayer (req, res, next) {
    // This doesn't actually get run, since ws overrides it at the root.
    if (chanPool[req.path] === undefined) return next();
  };
}
function wsUnit (server) {
  var chanPool = server.wsChannels = {};
  // Main WebSocket API:
  // ws(channel :: String, conListener :: function(socket))
  server.ws = function ws (channel, conListener) {
    if (channel[0] !== '/') {
      channel = '/$websocket:' + channel;  // Deprecated API.
    }
    if (chanPool[channel] !== undefined) {
      chanPool[channel].close();
    }
    chanPool[channel] = new WebSocket.Server({
      server: server,
      path: channel,
    });
    chanPool[channel].on('connection', conListener);
    return chanPool[channel];
  };
  // WebSocket broadcast API.
  // webBroadcast(channel :: String, recvListener :: function(data, end))
  server.wsBroadcast = function wsBroadcast (channel, recvListener) {
    if (channel[0] === '/') {
      return server.ws(channel, function (socket) {
        socket.on('message', function wsBroadcastRecv (data, flags) {
          recvListener({data: data, flags: flags}, {
            send: function wsBroadcastSend (dataBack) {
              chanPool[channel].clients.forEach(function (s) {
                s.send(dataBack);
              });
            },
          });
        });
      });
    } else { // Deprecated API
      return server.ws(channel, function (socket) {
        socket.on('message', function wsBroadcastRecv (data, flags) {
          recvListener(data, function wsBroadcastSend (dataBack) {
            chanPool[channel].clients.forEach(function (s) { s.send(dataBack); });
          });
        });
      });
    }
  };

  return function wsLayer (req, res, next) {
    // This doesn't actually get run, since ws overrides it at the root.
    if (chanPool[req.path] === undefined) return next();
  };
}
function wsUnit ( server ) { var chanPool = server . wsChannels = { } ; // Main WebSocket API: // ws(channel :: String, conListener :: function(socket)) server . ws = function ws ( channel , conListener ) { if ( channel [ 0 ] !== '/' ) { channel = '/$websocket:' + channel ; // Deprecated API. } if ( chanPool [ channel ] !== undefined ) { chanPool [ channel ] . close ( ) ; } chanPool [ channel ] = new WebSocket . Server ( { server : server , path : channel , } ) ; chanPool [ channel ] . on ( 'connection' , conListener ) ; return chanPool [ channel ] ; } ; // WebSocket broadcast API. // webBroadcast(channel :: String, recvListener :: function(data, end)) server . wsBroadcast = function wsBroadcast ( channel , recvListener ) { if ( channel [ 0 ] === '/' ) { return server . ws ( channel , function ( socket ) { socket . on ( 'message' , function wsBroadcastRecv ( data , flags ) { recvListener ( { data : data , flags : flags } , { send : function wsBroadcastSend ( dataBack ) { chanPool [ channel ] . clients . forEach ( function ( s ) { s . send ( dataBack ) ; } ) ; } , } ) ; } ) ; } ) ; } else { // Deprecated API return server . ws ( channel , function ( socket ) { socket . on ( 'message' , function wsBroadcastRecv ( data , flags ) { recvListener ( data , function wsBroadcastSend ( dataBack ) { chanPool [ channel ] . clients . forEach ( function ( s ) { s . send ( dataBack ) ; } ) ; } ) ; } ) ; } ) ; } } ; return function wsLayer ( req , res , next ) { // This doesn't actually get run, since ws overrides it at the root. if ( chanPool [ req . path ] === undefined ) return next ( ) ; } ; }
WebSocket unit.
WebSocket unit .