diff options
author | Chocobozzz <me@florianbigard.com> | 2020-09-25 10:04:21 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-11-09 15:33:04 +0100 |
commit | a5cf76afa378aae81af2a9b0ce548e5d2582f832 (patch) | |
tree | 58da320232bee7c9656774c5d6811e82bbf6c696 /server/lib/peertube-socket.ts | |
parent | de6310b2fcbb8a6b79c546b23dfa1920724faaa7 (diff) | |
download | PeerTube-a5cf76afa378aae81af2a9b0ce548e5d2582f832.tar.gz PeerTube-a5cf76afa378aae81af2a9b0ce548e5d2582f832.tar.zst PeerTube-a5cf76afa378aae81af2a9b0ce548e5d2582f832.zip |
Add watch messages if live has not started
Diffstat (limited to 'server/lib/peertube-socket.ts')
-rw-r--r-- | server/lib/peertube-socket.ts | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/server/lib/peertube-socket.ts b/server/lib/peertube-socket.ts index 2e4b15b38..c918a8685 100644 --- a/server/lib/peertube-socket.ts +++ b/server/lib/peertube-socket.ts | |||
@@ -1,14 +1,18 @@ | |||
1 | import * as SocketIO from 'socket.io' | 1 | import { Socket } from 'dgram' |
2 | import { authenticateSocket } from '../middlewares' | ||
3 | import { logger } from '../helpers/logger' | ||
4 | import { Server } from 'http' | 2 | import { Server } from 'http' |
3 | import * as SocketIO from 'socket.io' | ||
4 | import { MVideo } from '@server/types/models' | ||
5 | import { UserNotificationModelForApi } from '@server/types/models/user' | 5 | import { UserNotificationModelForApi } from '@server/types/models/user' |
6 | import { LiveVideoEventPayload, LiveVideoEventType } from '@shared/models' | ||
7 | import { logger } from '../helpers/logger' | ||
8 | import { authenticateSocket } from '../middlewares' | ||
6 | 9 | ||
7 | class PeerTubeSocket { | 10 | class PeerTubeSocket { |
8 | 11 | ||
9 | private static instance: PeerTubeSocket | 12 | private static instance: PeerTubeSocket |
10 | 13 | ||
11 | private userNotificationSockets: { [ userId: number ]: SocketIO.Socket[] } = {} | 14 | private userNotificationSockets: { [ userId: number ]: SocketIO.Socket[] } = {} |
15 | private liveVideosNamespace: SocketIO.Namespace | ||
12 | 16 | ||
13 | private constructor () {} | 17 | private constructor () {} |
14 | 18 | ||
@@ -32,19 +36,37 @@ class PeerTubeSocket { | |||
32 | this.userNotificationSockets[userId] = this.userNotificationSockets[userId].filter(s => s !== socket) | 36 | this.userNotificationSockets[userId] = this.userNotificationSockets[userId].filter(s => s !== socket) |
33 | }) | 37 | }) |
34 | }) | 38 | }) |
39 | |||
40 | this.liveVideosNamespace = io.of('/live-videos') | ||
41 | .on('connection', socket => { | ||
42 | socket.on('subscribe', ({ videoId }) => socket.join(videoId)) | ||
43 | socket.on('unsubscribe', ({ videoId }) => socket.leave(videoId)) | ||
44 | }) | ||
35 | } | 45 | } |
36 | 46 | ||
37 | sendNotification (userId: number, notification: UserNotificationModelForApi) { | 47 | sendNotification (userId: number, notification: UserNotificationModelForApi) { |
38 | const sockets = this.userNotificationSockets[userId] | 48 | const sockets = this.userNotificationSockets[userId] |
39 | |||
40 | if (!sockets) return | 49 | if (!sockets) return |
41 | 50 | ||
51 | logger.debug('Sending user notification to user %d.', userId) | ||
52 | |||
42 | const notificationMessage = notification.toFormattedJSON() | 53 | const notificationMessage = notification.toFormattedJSON() |
43 | for (const socket of sockets) { | 54 | for (const socket of sockets) { |
44 | socket.emit('new-notification', notificationMessage) | 55 | socket.emit('new-notification', notificationMessage) |
45 | } | 56 | } |
46 | } | 57 | } |
47 | 58 | ||
59 | sendVideoLiveNewState (video: MVideo) { | ||
60 | const data: LiveVideoEventPayload = { state: video.state } | ||
61 | const type: LiveVideoEventType = 'state-change' | ||
62 | |||
63 | logger.debug('Sending video live new state notification of %s.', video.url) | ||
64 | |||
65 | this.liveVideosNamespace | ||
66 | .in(video.id) | ||
67 | .emit(type, data) | ||
68 | } | ||
69 | |||
48 | static get Instance () { | 70 | static get Instance () { |
49 | return this.instance || (this.instance = new this()) | 71 | return this.instance || (this.instance = new this()) |
50 | } | 72 | } |