-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (49 loc) · 1.74 KB
/
index.js
File metadata and controls
59 lines (49 loc) · 1.74 KB
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
56
57
58
59
/**
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'cool game');
// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');
// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');
// sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'for your eyes only');
*/
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http, {pingTimeout: 30000});
let clientList = [];
//on connection event
io.on('connection', function(socket){
clientList.push(socket.id)
console.log(clientList);
/*
This method used to register expicitly
socket.on('registerToken', function(data){
})
*/
socket.on('sendMessage', function(messageBody){
//send to individual socket id or private message
//io.to(socketid).emit('onReceive', data);
console.log(messageBody);
data = messageBody;
//broadcast including client
//io.emit('onReceive', data);
socket.broadcast.emit('onReceive', data);
});
socket.on('disconnect', function(){
console.log('Disconnected : '+socket.id);
clientList.splice(clientList.indexOf(socket.id, 1))
})
});
//Port can be change from here
http.listen(3000, function(){
console.log('listening on *:3000');
});