messing around with things I don't understand
This commit is contained in:
parent
1569fec0cf
commit
aa0357e060
@ -1,15 +1,19 @@
|
||||
import { Server as WebSocketServer } from "ws";
|
||||
import { WebSocket, CLOSECODES, Payload, OPCODES } from "@fosscord/gateway";
|
||||
import { WebSocket, Payload, } from "@fosscord/gateway";
|
||||
import { Config, initDatabase } from "@fosscord/util";
|
||||
import OPCodeHandlers from "./opcodes";
|
||||
import { setHeartbeat } from "./util"
|
||||
import mediasoup from "mediasoup";
|
||||
import { setHeartbeat } from "./util";
|
||||
import * as mediasoup from "mediasoup";
|
||||
import { types as MediasoupTypes } from "mediasoup";
|
||||
|
||||
var port = Number(process.env.PORT);
|
||||
if (isNaN(port)) port = 3004;
|
||||
|
||||
export class Server {
|
||||
public ws: WebSocketServer;
|
||||
public mediasoupWorkers: MediasoupTypes.Worker[] = [];
|
||||
public mediasoupRouters: MediasoupTypes.Router[] = [];
|
||||
public mediasoupTransports: MediasoupTypes.Transport[] = [];
|
||||
|
||||
constructor() {
|
||||
this.ws = new WebSocketServer({
|
||||
@ -23,9 +27,9 @@ export class Server {
|
||||
const payload: Payload = JSON.parse(message);
|
||||
|
||||
if (OPCodeHandlers[payload.op])
|
||||
await OPCodeHandlers[payload.op](socket, payload);
|
||||
await OPCodeHandlers[payload.op].call(this, socket, payload);
|
||||
else
|
||||
console.error(`Unimplemented`, payload)
|
||||
console.error(`Unimplemented`, payload);
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -34,7 +38,63 @@ export class Server {
|
||||
// @ts-ignore
|
||||
await initDatabase();
|
||||
await Config.init();
|
||||
await this.createWorkers();
|
||||
console.log("[DB] connected");
|
||||
console.log(`[WebRTC] online on 0.0.0.0:${port}`);
|
||||
}
|
||||
|
||||
async createWorkers(): Promise<void> {
|
||||
const numWorkers = 1;
|
||||
for (let i = 0; i < numWorkers; i++) {
|
||||
const worker = await mediasoup.createWorker();
|
||||
if (!worker) return;
|
||||
|
||||
worker.on("died", () => {
|
||||
console.error("mediasoup worker died");
|
||||
});
|
||||
|
||||
worker.observer.on("newrouter", async (router: MediasoupTypes.Router) => {
|
||||
console.log("new router");
|
||||
|
||||
this.mediasoupRouters.push(router);
|
||||
|
||||
router.observer.on("newtransport", (transport: MediasoupTypes.Transport) => {
|
||||
console.log("new transport");
|
||||
|
||||
this.mediasoupTransports.push(transport);
|
||||
})
|
||||
|
||||
await router.createWebRtcTransport({
|
||||
listenIps: [{ ip: "127.0.0.1" }],
|
||||
enableUdp: true,
|
||||
enableTcp: true,
|
||||
preferUdp: true
|
||||
});
|
||||
});
|
||||
|
||||
await worker.createRouter({
|
||||
mediaCodecs: [
|
||||
{
|
||||
kind: "audio",
|
||||
mimeType: "audio/opus",
|
||||
clockRate: 48000,
|
||||
channels: 2
|
||||
},
|
||||
{
|
||||
kind: "video",
|
||||
mimeType: "video/H264",
|
||||
clockRate: 90000,
|
||||
parameters:
|
||||
{
|
||||
"packetization-mode": 1,
|
||||
"profile-level-id": "42e01f",
|
||||
"level-asymmetry-allowed": 1
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
this.mediasoupWorkers.push(worker);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { WebSocket } from "@fosscord/gateway";
|
||||
import { Payload } from "./index";
|
||||
import { Server } from "../Server"
|
||||
|
||||
export async function onConnect(socket: WebSocket, data: Payload) {
|
||||
export async function onConnect(this: Server, socket: WebSocket, data: Payload) {
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
import { WebSocket } from "@fosscord/gateway";
|
||||
import { Payload } from "./index";
|
||||
import { setHeartbeat } from "./../util";
|
||||
import { Server } from "../Server"
|
||||
|
||||
export async function onHeartbeat(socket: WebSocket, data: Payload) {
|
||||
export async function onHeartbeat(this: Server, socket: WebSocket, data: Payload) {
|
||||
await setHeartbeat(socket);
|
||||
}
|
@ -1,14 +1,17 @@
|
||||
import { WebSocket } from "@fosscord/gateway";
|
||||
import { Payload } from "./index"
|
||||
import { VoiceOPCodes } from "@fosscord/util";
|
||||
import { Server } from "../Server"
|
||||
|
||||
export async function onIdentify(socket: WebSocket, data: Payload) {
|
||||
export async function onIdentify(this: Server, socket: WebSocket, data: Payload) {
|
||||
socket.send(JSON.stringify({
|
||||
op: VoiceOPCodes.READY,
|
||||
d: {
|
||||
ssrc: 1,
|
||||
ip: "127.0.0.1",
|
||||
port: 3005,
|
||||
|
||||
//@ts-ignore
|
||||
port: this.mediasoupTransports[0].iceCandidates.port,
|
||||
modes: [
|
||||
"xsalsa20_poly1305",
|
||||
"xsalsa20_poly1305_suffix",
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { WebSocket } from "@fosscord/gateway";
|
||||
import { Payload } from "./index";
|
||||
import { Server } from "../Server"
|
||||
|
||||
export async function onResume(socket: WebSocket, data: Payload) {
|
||||
export async function onResume(this: Server, socket: WebSocket, data: Payload) {
|
||||
}
|
@ -1,15 +1,16 @@
|
||||
import { WebSocket } from "@fosscord/gateway";
|
||||
import { Payload } from "./index";
|
||||
import { VoiceOPCodes } from "@fosscord/util";
|
||||
import { Server } from "../Server"
|
||||
|
||||
export async function onSelectProtocol(socket: WebSocket, data: Payload) {
|
||||
export async function onSelectProtocol(this: Server, 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",
|
||||
media_session_id: this.mediasoupTransports[0].id,
|
||||
audio_codec: "opus",
|
||||
}
|
||||
}));
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { WebSocket } from "@fosscord/gateway";
|
||||
import { Payload } from "./index"
|
||||
import { VoiceOPCodes } from "@fosscord/util";
|
||||
import { Server } from "../Server"
|
||||
|
||||
export async function onSpeaking(socket: WebSocket, data: Payload) {
|
||||
export async function onSpeaking(this: Server, socket: WebSocket, data: Payload) {
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user