index.js (859B)
1 var app = require('express')(); 2 var http = require('http').Server(app); 3 var io = require('socket.io')(http); 4 var port = process.env.PORT || 3000; 5 6 app.get('/', function(req, res){ 7 res.sendFile(__dirname + '/index.html'); 8 }); 9 10 io.on('connection', function(socket){ 11 socket.on('username', function(username){ 12 socket.username = username; 13 var address = socket.request.connection.remoteAddress; 14 io.emit('is_online', '🔵 <i>' + socket.username + ' (' + address + ') joined the chat..</i>'); 15 }); 16 17 18 socket.on('disconnect', function(username) { 19 io.emit('is_online', '🔴 <i>' + socket.username + ' left the chat..</i>'); 20 }); 21 22 socket.on('chat message', function(msg){ 23 24 io.emit('chat message','▒ '+ socket.username + ': ' + msg); 25 }); 26 27 }); 28 29 http.listen(port, function(){ 30 console.log('you are now chatting on port ' + port); 31 });