-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_server.js
31 lines (26 loc) · 880 Bytes
/
ws_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
const co = require('co')
// const ws = require('ws')
const socketServer = require('socket.io')
const Cookies = require('cookies')
const cookieParser = require('cookie-parser')
const wsManager = require('./service').wsManager
module.exports = (httpServer, sessionStore, keys) => {
const io = socketServer(httpServer)
io.on('connection', async(socket) => {
const cookies = new Cookies(socket.request, null)
if (cookies.get('connect.sid')) {
const sid = cookies.get('connect.sid').split('s%3A')[1].split('.')[0]
var session = await wsManager.getUser(sessionStore, sid)
if (session.userid) {
var username = session.username
socket.username = username
socket.userid = session.userid
wsManager.put(socket.username, socket)
} else {
socket.onclose()
}
} else {
socket.onclose()
}
})
}