-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (61 loc) · 2.27 KB
/
server.js
File metadata and controls
71 lines (61 loc) · 2.27 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Server.js manages the link between player groups, websocket messages sent and audio processor.
*/
import {WebSocketHandler} from "./wshandler.js";
import {AudioProcessor} from "./audio/audioprocessor.js";
import {PlayerManager} from "./players/playermanager.js";
export class Server {
constructor(
websocket = new WebSocketHandler(this),
audio = new AudioProcessor(this),
players = new PlayerManager(this)
) {
this.websocket = websocket;
this.audio = audio;
this.players = players;
}
processMessage(username, data) {
let message;
try {
message = JSON.parse(data);
} catch (e) {
throw new Error("Invalid JSON data");
}
if (!message.type || !message.data) {
throw new Error("Message type or data is missing");
}
try {
switch (message.type) {
case "audio_data":
this.audio.processAudioData(username, message.data);
break;
case "channel_data":
this.players.processChannelChange(username, message.data);
break;
case "permissions":
this.players.managePermissions(username, message.data);
break;
case "channels":
this.players.manageChannels(username, message.data);
break;
default:
throw new Error("Unknown message type: " + message.type);
}
} catch (e) {
console.error("Error in Server on request type " + message.type + " -> " + e);
this.sendDeniedMessage(username, "Error processing request");
}
}
sendAudioData(username, data) {
this.websocket.sendMessageToUser(username, {type: "audio_data", data});
}
sendSuccessMessage(username, message) {
this.websocket.sendMessageToUser(username, {type: "request_success", message});
}
sendDeniedMessage(username, message) {
this.websocket.sendMessageToUser(username, {type: "request_denied", message});
}
sendUpdatedPermissions(username, data) {
this.websocket.sendMessageToUser(username, {type: "permission_updated", data});
}
}