-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessages.ts
More file actions
39 lines (37 loc) · 960 Bytes
/
messages.ts
File metadata and controls
39 lines (37 loc) · 960 Bytes
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
32
33
34
35
36
37
38
39
export interface PlayUrl {
type: 'play_url';
url: string;
}
export interface PlayText {
type: 'play_text';
text: string;
volume: number;
}
export interface CloseCmd {
type: 'close';
}
export interface MessageHandlers {
onPlayUrl: (cmd: PlayUrl) => void;
onPlayText: (cmd: PlayText) => void;
onClose: (cmd: CloseCmd) => void;
}
export function handleMessage(raw: string, handlers: MessageHandlers): void {
let cmd: unknown;
try {
cmd = JSON.parse(raw);
} catch {
console.warn('Ignoring malformed relay message');
return;
}
if (!cmd || typeof cmd !== 'object' || !('type' in cmd)) return;
switch ((cmd as { type: string }).type) {
case 'play_url':
return handlers.onPlayUrl(cmd as PlayUrl);
case 'play_text':
return handlers.onPlayText(cmd as PlayText);
case 'close':
return handlers.onClose(cmd as CloseCmd);
default:
console.warn('Ignoring unknown relay message type');
}
}