-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
38 lines (34 loc) · 1005 Bytes
/
index.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
import ws from 'ws'
import GlobalEmitService from './service/GlobalEmitService'
import DataService from './service/DataService'
import ConnectController from './controller/ConnectController'
const wsServer = ws.Server({ port: 9001 })
const globalMap = {
userMap: {},
roomMap: {},
roomUser: {},
gameMap: {}
}
function deleteExpireUser () {
const allUsers = Object.values(globalMap.userMap)
const nowTime = new Date().getTime()
allUsers.forEach(u => {
if (!u.isOnline) {
const timeNotTouch = nowTime - u.lastLoginTime
if (timeNotTouch > 1000 * 60 * 60 * 2) {
console.log('user not touch ', timeNotTouch)
u.inGame = false
u.currentRoomId = ''
delete globalMap.userMap[u.token]
console.log('delete user from userMap: ', u.username)
}
}
})
}
setInterval(_ => {
deleteExpireUser()
}, 100000)
wsServer.on('connection', ws => {
new ConnectController(ws, globalMap)
})
console.log('websocket start:', wsServer.options.port)