How to use the websocket.io.attach function in websocket

To help you get started, we’ve selected a few websocket examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github popeindustries / buddy / lib / utils / ReloadServer.js View on Github external
if (err) return fn(err);
						response.writeHead(200, {
							'Content-Length': data.length,
							'Content-Type': 'text/javascript'
						});
						response.end(data);
					});
				// All other requests 404
				} else {
					response.writeHead(404);
					response.end();
				}
			});
		});
		// Create socket server
		self.wsServer = ws.attach(self.server);
		self.wsServer.on('connection', function(socket) {
			self._createConnection(socket);
		});
		fn();
	});
};
github remy / remote-tilt / server.js View on Github external
app.get('/:key', function (req, res, next) {
  var key = req.params.key;
  if (connections[key]) {
    res.render('remote', {
      key: key
    });
  } else {
    res.render('index', {
      error: 'The key requested "' + key + '" is not being listened for. Fire up your client again.'
    });
  }
});


var server = ws.attach(app),
    LISTEN = 1,
    SERVE = 2;

server.on('connection', function (socket) {
  var url = parse(socket.req.url);
  var key = path.basename(url.pathname),
      type = LISTEN;
  
  if (url.pathname.indexOf('/listen/') === 0) {
    if (!connections[key]) connections[key] = [];
    connections[key].push(socket);
  } else if (url.pathname.indexOf('/serve/') === 0) {
    type = SERVE;
  }

  console.log((type == LISTEN ? 'listening: ' : 'serving: ') + key + ' - ' + (new Date));