-
Notifications
You must be signed in to change notification settings - Fork 6
/
socketio.js
80 lines (74 loc) · 2.24 KB
/
socketio.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module.exports = function(server) {
var io = require('socket.io')(server, {
transports: ['polling', 'websocket'],
allowUpgrades: true
});
// Simple Example
io.on('connection', function(socket) {
socket.on('message', function(msg) {
socket.broadcast.send(msg);
});
socket.on('disconnect', function() {
socket.broadcast.send('disconnected');
});
});
// Namespace Example
var namespace = io
.of('/namespace')
.on('connection', function(socket) {
socket.on('message', function(data) {
socket.broadcast.send(data);
});
socket.on('disconnect', function() {
socket.broadcast.send('disconnected');
});
});
// Custom Example
var custom = io
.of('/custom')
.on('connection', function(socket) {
socket.on('fromclient', function(data) {
socket.broadcast.emit('fromserver', data);
});
socket.on('disconnect', function() {
socket.broadcast.emit('fromserver', 'disconnected');
});
});
// Volatile Example
var volatile = io
.of('/volatile')
.on('connection', function(socket) {
var vola = setInterval(function () {
socket.volatile.emit('current time', 'current time : ' + new Date());
}, 2000);
socket.on('disconnect', function () {
clearInterval(vola);
socket.volatile.emit('current time', 'disconnected');
});
});
// room Example
var Room = io
.of('/room')
.on('connection', function(socket) {
var joinedRoom = null;
socket.on('join room', function(data) {
if (!!joinedRoom) {
socket.leave(joinedRoom);
socket.emit('joined', 'you\'ve left from ' + joinedRoom);
socket.broadcast.to(joinedRoom).send('someone left this room.');
joinedRoom = null;
}
socket.join(data);
joinedRoom = data;
socket.emit('joined', 'you\'ve joined to ' + data);
socket.broadcast.to(joinedRoom).send('someone joined room.');
});
socket.on('fromclient', function(data) {
if (joinedRoom) {
socket.broadcast.to(joinedRoom).send(data);
} else {
socket.send('you\'re not joined a room. select a room and then push join.');
}
});
});
}