Shivam commited on
Commit
ae19567
·
1 Parent(s): 1288eae

Describe what you changed

Browse files
Files changed (3) hide show
  1. lib/room.ts +2 -0
  2. lib/sockets.ts +85 -0
  3. lib/types.ts +12 -0
lib/room.ts CHANGED
@@ -78,5 +78,7 @@ export const createNewRoom = async (roomId: string, socketId: string) => {
78
  lastSync: new Date().getTime() / 1000,
79
  },
80
  users: [],
 
 
81
  })
82
  }
 
78
  lastSync: new Date().getTime() / 1000,
79
  },
80
  users: [],
81
+ // Initialize chat log for in-room chat feature
82
+ chatLog: [],
83
  })
84
  }
lib/sockets.ts ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ MediaElement,
3
+ PlayerState,
4
+ Playlist,
5
+ RoomState,
6
+ UserState,
7
+ ChatMessage,
8
+ } from "./types"
9
+ import io, { Socket } from "socket.io-client"
10
+
11
+ export interface ServerToClientEvents {
12
+ playlistUpdate: (playlist: Playlist) => void
13
+ userUpdates: (users: UserState[]) => void
14
+ update: (room: RoomState) => void
15
+
16
+ // Chat events
17
+ chatNew: (msg: ChatMessage) => void
18
+ chatHistory: (msgs: ChatMessage[]) => void
19
+ }
20
+
21
+ export interface ClientToServerEvents {
22
+ playItemFromPlaylist: (index: number) => void
23
+ updatePlaylist: (playlist: Playlist) => void
24
+ updatePlayer: (player: PlayerState) => void
25
+ updatePlaying: (playing: MediaElement) => void
26
+ updateUser: (user: UserState) => void
27
+
28
+ setPaused: (paused: boolean) => void
29
+ setLoop: (loop: boolean) => void
30
+ setProgress: (progress: number) => void
31
+ setPlaybackRate: (playbackRate: number) => void
32
+
33
+ seek: (progress: number) => void
34
+ playUrl: (src: string) => void
35
+ playAgain: () => void
36
+ playEnded: () => void
37
+ fetch: () => void
38
+ error: () => void
39
+
40
+ // Chat: client -> server
41
+ chatMessage: (text: string) => void
42
+ }
43
+
44
+ export function playItemFromPlaylist(
45
+ socket: Socket<ServerToClientEvents, ClientToServerEvents>,
46
+ playlist: Playlist,
47
+ index: number
48
+ ) {
49
+ if (
50
+ typeof playlist.items[index] === "undefined" ||
51
+ playlist.items[index] === null
52
+ ) {
53
+ console.error("Impossible to play", index, "from", playlist)
54
+ return
55
+ }
56
+ socket.emit("playItemFromPlaylist", index)
57
+ }
58
+
59
+ export function createClientSocket(roomId: string) {
60
+ console.log("Trying to join room", roomId)
61
+ const socket = io({
62
+ query: {
63
+ roomId,
64
+ },
65
+ transports: ["websocket"],
66
+ path: "/api/socketio",
67
+ })
68
+
69
+ socket.on("connect", () => {
70
+ console.log("Established ws connection to io server", socket.id)
71
+ })
72
+
73
+ socket.on("disconnect", (reason) => {
74
+ if (!["io client disconnect", "io server disconnect"].includes(reason)) {
75
+ console.error(
76
+ "Socket connection closed due to:",
77
+ reason,
78
+ "socket:",
79
+ socket
80
+ )
81
+ }
82
+ })
83
+
84
+ return socket
85
+ }
lib/types.ts CHANGED
@@ -62,6 +62,17 @@ export interface CommandLog {
62
  time: number
63
  }
64
 
 
 
 
 
 
 
 
 
 
 
 
65
  export interface RoomState {
66
  serverTime: number
67
  id: string
@@ -69,4 +80,5 @@ export interface RoomState {
69
  users: UserState[]
70
  targetState: TargetState
71
  commandHistory: CommandLog[]
 
72
  }
 
62
  time: number
63
  }
64
 
65
+ /**
66
+ * Chat message structure for in-room chat
67
+ */
68
+ export interface ChatMessage {
69
+ id: string // unique id per message
70
+ userId: string // sender socket/user id
71
+ name: string // sender display name
72
+ text: string // message text
73
+ ts: number // timestamp (ms since epoch)
74
+ }
75
+
76
  export interface RoomState {
77
  serverTime: number
78
  id: string
 
80
  users: UserState[]
81
  targetState: TargetState
82
  commandHistory: CommandLog[]
83
+ chatLog?: ChatMessage[] // optional to keep backward compatibility
84
  }