Advertisement
Underworld1337

Untitled

Sep 13th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Require HTTP module (to start server) and Socket.IO
  2. var http = require('http');
  3. var io = require('socket.io');
  4. var port = 8080;
  5.  
  6. // Start the server at port 8080
  7. var server = http.createServer(function(req, res){
  8.     // Send HTML headers and message
  9.     res.writeHead(200,{ 'Content-Type': 'text/html' });
  10.     res.end('<h1>Hello Socket Lover!</h1>');
  11. });
  12.  
  13. server.listen(port);
  14.  
  15. // Create a Socket.IO instance, passing it our server
  16. var socket = io.listen(server);
  17.  
  18. // Add a connect listener
  19. socket.on('connection', function(client){
  20.     console.log('Connection to client established');
  21.  
  22.     // Success!  Now listen to messages to be received
  23.     client.on('message',function(event){
  24.         console.log('Received message from client!',event);
  25.     });
  26.  
  27.     client.on('disconnect',function(){
  28.         clearInterval(interval);
  29.         console.log('Server has disconnected');
  30.     });
  31. });
  32.  
  33. console.log('Server running at http://127.0.0.1:' + port + '/');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement