-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (30 loc) · 813 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
31
32
33
34
35
36
37
38
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');
});
io.on('connection', function(socket){
var user_id = Math.floor((Math.random() * 10000) + 1);
socket.emit('my id',user_id); // Emits the user id
socket.on('chat message', function(msg){
var data = {
user_id : user_id,
msg: msg
};
io.emit('chat message', data);
});
socket.on('connection success', function(msg){
var data = {
user_id : user_id,
msg: msg
};
io.emit('connection success', data);
});
socket.on('disconnect', function(msg){
io.emit('disconnected','user-' + user_id);
});
});
http.listen(3000, function(){
console.log('listening on localhost:3000');
});