Fix upgrade server session

This commit is contained in:
Evert Prants 2019-10-25 15:32:24 +03:00
parent c2cbd60f96
commit 295a9cfd1b
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

15
app.js
View File

@ -432,10 +432,15 @@ app.use((error, req, res, next) => {
// Socket Server
wss.on('connection', (ws, request, client) => {
const userId = request.user.uuid || request.session.id
const username = request.user.username
let userId = request.session.id
let username = 'A Friendly Guest'
let myChannels = []
if (request.user) {
userId = request.user.uuid
username = request.user.username
}
dev && console.log(userId, 'connected')
ws.on('message', (msg) => {
dev && console.log(userId, 'said', msg)
@ -447,7 +452,7 @@ wss.on('connection', (ws, request, client) => {
dev && console.log('adding watcher', userId, 'to channel', chan)
if (cache.live.indexOf(chan) !== -1) {
if (!cache.viewers[chan]) cache.viewers[chan] = {}
cache.viewers[chan][userId] = username || 'A Friendly Guest'
cache.viewers[chan][userId] = username
if (myChannels.indexOf(chan) === -1) myChannels.push(chan)
}
break
@ -481,6 +486,10 @@ wss.on('connection', (ws, request, client) => {
// Handle upgrade, parse included session
server.on('upgrade', (request, socket, head) => {
sessionParser(request, {}, () => {
if (request.session && request.session.passport) {
request.user = request.session.passport.user
}
wss.handleUpgrade(request, socket, head, function(ws) {
wss.emit('connection', ws, request)
})