Speed up development with full-stack environments for every branch.

Learn More

Using the Node HTTP server [Socket.IO and Node.js]

619 Runs 4985 Views 1876 Copies
Saved

Saved

sundip 66

sundip
published 4 years ago

/**
 * More details here: http://socket.io/#how-to-use 
 */

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs');

app.listen( 80 );

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(data);
  });
}

io.set('log level', 1);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).