-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
34 lines (29 loc) · 890 Bytes
/
app.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
31
32
33
34
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var clients = {};
app.get('/', function(req, res){
res.send('server is running');
});
io.on('connection', function(client){
client.on('join', function(name){
console.log('Join: ' + name);
clients[client.id] = name;
client.emit("update", "You have connected to the server.");
client.broadcast.emit("update", name + " has joined the server.");
});
client.on('send', function(msg){
console.log('Message: ' + msg);
client.broadcast.emit("chat", clients[client.id], msg);
});
client.on('disconnect', function(){
if (clients[client.id] != undefined) {
console.log('Disconnect');
io.emit("update", clients[client.id] + " has left the server.");
}
delete clients[client.id];
});
});
http.listen(3000, function(){
console.log('listening port 3000');
});