-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
40 lines (32 loc) · 1.2 KB
/
socket.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
var socketiO = {
clients : [] ,
setServer : function(listenningServer) {this.listenningServer = listenningServer;},
setSession : function(session) {this.session = session},
run : function(app,callback) {
this.io = require('socket.io').listen(this.listenningServer);
var sharedsession = require("express-socket.io-session");
this.io.use(sharedsession(this.session,{autoSave:true}));
this.io.on('connection', function (socket) {
socket.on('check',(msg)=>{
if(socketiO.clients.indexOf(socket.handshake.session.user)!== -1)
socketiO.clients.push(socket.handshake.session.user);
if (socket.handshake.session.c == 0) {
socket.broadcast.emit("nouveau" , socket.handshake.session.user);
socket.handshake.session.c += 1;
socket.handshake.session.save();
}
});
socket.on('msg',function(msg){
socket.broadcast.emit('msg',socket.handshake.session.user+" "+ msg);
});
socket.on("pseudo",function(pseudo){
socket.broadcast.emit("nouveau",pseudo);
});
});
app.logout.socket = app.socket;
app.login.socket = app.socket;
if (callback && typeof(callback) === "function")
callback();
}
}
module.exports = socketiO;