boilerplate stuff

This commit is contained in:
Madeline
2022-01-16 02:38:43 +11:00
parent 41fca13db2
commit 1569fec0cf
17 changed files with 556 additions and 244 deletions
+15 -21
View File
@@ -1,5 +1,8 @@
import { Server as WebSocketServer } from "ws";
import { Config, db } from "@fosscord/util";
import { WebSocket, CLOSECODES, Payload, OPCODES } from "@fosscord/gateway";
import { Config, initDatabase } from "@fosscord/util";
import OPCodeHandlers from "./opcodes";
import { setHeartbeat } from "./util"
import mediasoup from "mediasoup";
var port = Number(process.env.PORT);
@@ -7,38 +10,29 @@ if (isNaN(port)) port = 3004;
export class Server {
public ws: WebSocketServer;
public turn: any;
constructor() {
this.ws = new WebSocketServer({
port,
maxPayload: 4096,
});
this.ws.on("connection", (socket) => {
socket.on("message", (message) => {
socket.emit(
JSON.stringify({
op: 2,
d: {
ssrc: 1,
ip: "127.0.0.1",
port: 3004,
modes: [
"xsalsa20_poly1305",
"xsalsa20_poly1305_suffix",
"xsalsa20_poly1305_lite",
],
heartbeat_interval: 1,
},
})
);
this.ws.on("connection", async (socket: WebSocket) => {
await setHeartbeat(socket);
socket.on("message", async (message: string) => {
const payload: Payload = JSON.parse(message);
if (OPCodeHandlers[payload.op])
await OPCodeHandlers[payload.op](socket, payload);
else
console.error(`Unimplemented`, payload)
});
});
}
async listen(): Promise<void> {
// @ts-ignore
await (db as Promise<Connection>);
await initDatabase();
await Config.init();
console.log("[DB] connected");
console.log(`[WebRTC] online on 0.0.0.0:${port}`);
+5
View File
@@ -0,0 +1,5 @@
import { WebSocket } from "@fosscord/gateway";
import { Payload } from "./index";
export async function onConnect(socket: WebSocket, data: Payload) {
}
+7
View File
@@ -0,0 +1,7 @@
import { WebSocket } from "@fosscord/gateway";
import { Payload } from "./index";
import { setHeartbeat } from "./../util";
export async function onHeartbeat(socket: WebSocket, data: Payload) {
await setHeartbeat(socket);
}
+20
View File
@@ -0,0 +1,20 @@
import { WebSocket } from "@fosscord/gateway";
import { Payload } from "./index"
import { VoiceOPCodes } from "@fosscord/util";
export async function onIdentify(socket: WebSocket, data: Payload) {
socket.send(JSON.stringify({
op: VoiceOPCodes.READY,
d: {
ssrc: 1,
ip: "127.0.0.1",
port: 3005,
modes: [
"xsalsa20_poly1305",
"xsalsa20_poly1305_suffix",
"xsalsa20_poly1305_lite",
],
heartbeat_interval: 1,
},
}));
}
+5
View File
@@ -0,0 +1,5 @@
import { WebSocket } from "@fosscord/gateway";
import { Payload } from "./index";
export async function onResume(socket: WebSocket, data: Payload) {
}
+16
View File
@@ -0,0 +1,16 @@
import { WebSocket } from "@fosscord/gateway";
import { Payload } from "./index";
import { VoiceOPCodes } from "@fosscord/util";
export async function onSelectProtocol(socket: WebSocket, data: Payload) {
socket.send(JSON.stringify({
op: VoiceOPCodes.SESSION_DESCRIPTION,
d: {
video_codec: "H264",
secret_key: new Array(32).fill(null).map(x => Math.random() * 256),
mode: "aead_aes256_gcm_rtpsize",
media_session_id: "d8eb5c84d987c6642ec4ce72ffa97f00",
audio_codec: "opus",
}
}));
}
+6
View File
@@ -0,0 +1,6 @@
import { WebSocket } from "@fosscord/gateway";
import { Payload } from "./index"
import { VoiceOPCodes } from "@fosscord/util";
export async function onSpeaking(socket: WebSocket, data: Payload) {
}
+35
View File
@@ -0,0 +1,35 @@
import { WebSocket } from "@fosscord/gateway";
import { VoiceOPCodes } from "@fosscord/util";
export interface Payload {
op: number;
d?: any;
s?: number;
t?: string;
}
import { onIdentify } from "./Identify";
import { onSelectProtocol } from "./SelectProtocol";
import { onHeartbeat } from "./Heartbeat";
import { onSpeaking } from "./Speaking";
import { onResume } from "./Resume";
import { onConnect } from "./Connect";
export type OPCodeHandler = (this: WebSocket, data: Payload) => any;
export default {
[VoiceOPCodes.IDENTIFY]: onIdentify, //op 0
[VoiceOPCodes.SELECT_PROTOCOL]: onSelectProtocol, //op 1
//op 2 voice_ready
[VoiceOPCodes.HEARTBEAT]: onHeartbeat, //op 3
//op 4 session_description
[VoiceOPCodes.SPEAKING]: onSpeaking, //op 5
//op 6 heartbeat_ack
[VoiceOPCodes.RESUME]: onResume, //op 7
//op 8 hello
//op 9 resumed
//op 10?
//op 11?
[VoiceOPCodes.CLIENT_CONNECT]: onConnect, //op 12
//op 13?
};
+1
View File
@@ -1,3 +1,4 @@
import { Server } from "./Server";
const server = new Server();
server.listen();
+18
View File
@@ -0,0 +1,18 @@
import { WebSocket, CLOSECODES } from "@fosscord/gateway";
import { VoiceOPCodes } from "@fosscord/util";
export async function setHeartbeat(socket: WebSocket) {
if (socket.heartbeatTimeout) clearTimeout(socket.heartbeatTimeout);
socket.heartbeatTimeout = setTimeout(() => {
return socket.close(CLOSECODES.Session_timed_out);
}, 1000 * 45);
socket.send(JSON.stringify({
op: VoiceOPCodes.HEARTBEAT_ACK,
d: {
v: 6,
heartbeat_interval: 13750,
}
}));
}
+1
View File
@@ -0,0 +1 @@
export * from "./Heartbeat"