-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (24 loc) · 849 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
// User connects to socket
io.on('connection', function(socket){
socket.broadcast.emit('user connected', 'a user connected: ' + io.engine.clientsCount);
console.log('a user connected: ' + io.engine.clientsCount);
// User disconnects from socket
socket.on('disconnect', function(){
console.log('user disconnected: ' + io.engine.clientsCount);
});
// Server receives message
socket.on('chat message', function(msg){
// Server broadcasts message
io.emit('chat message', msg);
console.log('message: ' + msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});