Fix socket notification with multiple user tabs
This commit is contained in:
parent
916937d7da
commit
1b42d73f44
1 changed files with 10 additions and 6 deletions
|
@ -8,7 +8,7 @@ class PeerTubeSocket {
|
|||
|
||||
private static instance: PeerTubeSocket
|
||||
|
||||
private userNotificationSockets: { [ userId: number ]: SocketIO.Socket } = {}
|
||||
private userNotificationSockets: { [ userId: number ]: SocketIO.Socket[] } = {}
|
||||
|
||||
private constructor () {}
|
||||
|
||||
|
@ -22,23 +22,27 @@ class PeerTubeSocket {
|
|||
|
||||
logger.debug('User %d connected on the notification system.', userId)
|
||||
|
||||
this.userNotificationSockets[userId] = socket
|
||||
if (!this.userNotificationSockets[userId]) this.userNotificationSockets[userId] = []
|
||||
|
||||
this.userNotificationSockets[userId].push(socket)
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
logger.debug('User %d disconnected from SocketIO notifications.', userId)
|
||||
|
||||
delete this.userNotificationSockets[userId]
|
||||
this.userNotificationSockets[userId] = this.userNotificationSockets[userId].filter(s => s !== socket)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
sendNotification (userId: number, notification: UserNotificationModel) {
|
||||
const socket = this.userNotificationSockets[userId]
|
||||
const sockets = this.userNotificationSockets[userId]
|
||||
|
||||
if (!socket) return
|
||||
if (!sockets) return
|
||||
|
||||
for (const socket of sockets) {
|
||||
socket.emit('new-notification', notification.toFormattedJSON())
|
||||
}
|
||||
}
|
||||
|
||||
static get Instance () {
|
||||
return this.instance || (this.instance = new this())
|
||||
|
|
Loading…
Reference in a new issue