-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (71 loc) · 2.45 KB
/
server.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
const express = require('express')
const app = express()
const port = process.env.PORT || 5000
const server = app.listen(port, () => console.log(`Server running on port ${port}`))
const io = require('socket.io')(server)
const { addUser, getUser, getAllUsers } = require('./socketUsers.js')
let messages = [], online = [], unread = []
io.on('connection', socket => {
socket.on('join', ({ id, username, nicname }) => {
const userId = addUser(id, username, nicname)
if (userId !== undefined) {
socket.join(userId)
const getData = getUser(userId)[0]
io.to(userId).emit('joined', getData)
}
})
socket.on('online', id => {
let check = online.filter(o => o.id === id)
if (!check.length > 0)
online.push({ id, date: new Date, status: true })
else {
check = online.filter(o => {
if (o.id === id) {
o.date = new Date
o.status = true
return o
} else return o
})
online = check
}
io.emit('serverOnline', online)
})
socket.on('getOnline', (id) => {
io.to(id).emit('serverOnline', online)
})
socket.on('rmonline', id => {
const check = online.filter(o => {
if (o.id === id) {
o.status = false
return o
} else return o
})
online = check
io.emit('serverOnline', online)
})
socket.on('userdata', ({ id, userId }) => {
let data = getUser(id)[0]
io.to(userId).emit('receivedData', data)
})
socket.on('allusers', () => {
let users = getAllUsers()
io.emit('serverallusers', users)
})
socket.on('getmessages', id => {
io.to(id).emit('message-server', messages)
})
socket.on('unread', id => {
io.to(id).emit('unread-server', unread)
})
socket.on('rmunread', ({ userId, to }) => {
const rm = unread.filter(r => !((r.to === userId) && (r.senderId === to)))
unread = rm
io.to(userId).emit('unread-server', unread)
})
socket.on('message-client', ({ senderId, username, to, nicname, message, date }) => {
unread.push({ senderId, to, username, nicname, message, date })
messages.push({ senderId, to, username, nicname, message, date })
io.emit('unread-server', unread)
io.emit('message-server', messages)
})
})