-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
78 lines (30 loc) · 1.02 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
console.log('starting chat server...');
const http = require('http');
const hostname = '10.0.0.101';
const port = '8000';
console.log('creating server...')
const server = http.createServer().listen(port, hostname);
console.log('chat server running on '+ hostname + ':' + port);
var socketList = require('socket.io').listen(server);
// detect every new socket connection
socketList.sockets.on('connection', function(socket){
console.log('new connection received');
// detect disconnect of this socket
socket.on('disconnect', function(){
console.log('disconnected socket');
});
socket.on('join_room', function(data){
socket.join(data.chatroom);
socketList.in(data.chatroom).emit('user_joined', {
user_email: data.user_email,
chatroom: data.chatroom
});
});
socket.on('send_message', function(data){
console.log(data.message, data.user_email);
socketList.in(data.chatroom).emit('receive_message', {
message: data.message,
user_email: data.user_email
});
});
});