// using express to handle routing var express = require('express'); var app = express(); // http server for loading html pages var http = require('http').Server(app); // socket.io for handling websockets var io = require('socket.io')(http) var path = require('path'); app.get('/', function(req, res){ res.sendFile(path.join(__dirname, 'index.html')); }); // start node server http.listen(8888, function(){ console.log('listening on *:8888'); }); app.use(express.static(__dirname)); // event handler for when a client connects io.on('connection', function(socket){ // socket ID is a unique identifier for this particular connection // it assigns a new one for every new browser connection, even if it's the same user. console.log(socket.id, "connected"); // generate random hex color var color = ''+(Math.random()*0xFFFFFF<<0).toString(16); // cookies can be accessed as per usual from a socket connection console.log("cookies", socket.request.headers.cookie); // listen for messages from this client socket.on('newmessage', function(msg){ console.log(msg); // broadcast the received message to all the other clients socket.broadcast.emit("newmessage", msg); }); socket.on('cursor', function(msg){ msg.color = color; // broadcast the message to ALL clients (including the sender) io.sockets.emit("cursor", msg); }); socket.on('disconnect', function() { // remove this socket object from current online list console.log("disconnected", socket.id); }); });