forked from ehsanmajd/chat-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
25 lines (20 loc) · 805 Bytes
/
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
const userConnectionManager = require('./repo/userConnections');
const chatManager = require('./repo/chats');
module.exports = server => {
const io = require('socket.io')(server);
chatManager.setSocket(io);
io.on('connection', (socket) => {
socket.on('online', userId => {
console.log(`userId ${userId} is online. socketId: ${socket.id}`);
userConnectionManager.submitConnection(userId, socket.id, socket)
});
socket.on('offline', userId => {
console.log(`userId ${userId} is offline. socketId: ${socket.id}`);
userConnectionManager.signOut(userId);
});
socket.on('disconnect', reason => {
console.log(`a user got disconnected. reason: ${reason} socketId: ${socket.id}`);
userConnectionManager.removeConnection(socket.id);
});
});
}