start.ts file

This commit is contained in:
Flam3rboy 2021-05-31 20:32:03 +02:00
parent e12197d568
commit 5856047e3d
3 changed files with 37 additions and 17 deletions

View File

@ -4,25 +4,21 @@ dotenv.config();
import { Config, db } from "@fosscord/server-util"; import { Config, db } from "@fosscord/server-util";
import { Server as WebSocketServer } from "ws"; import { Server as WebSocketServer } from "ws";
import { Connection } from "./events/Connection"; import { Connection } from "./events/Connection";
import http from "http";
// TODO: only listen/start the server if everything got initalized
// https://www.npmjs.com/package/ws use "External HTTP/S server" and listen manually at the end of listen()
var port = Number(process.env.PORT);
if (isNaN(port)) port = 3002;
export class Server { export class Server {
public ws: WebSocketServer; public ws: WebSocketServer;
constructor() { public port: number;
this.ws = new WebSocketServer({ public server: http.Server;
port,
constructor({ port, server }: { port: number; server: http.Server }) {
this.port = port;
if (server) this.server = server;
else this.server = http.createServer({});
this.ws = new WebSocketServer({
maxPayload: 4096, maxPayload: 4096,
// perMessageDeflate: { server: this.server,
// zlibDeflateOptions: {
// chunkSize: 65536,
// },
// },
}); });
this.ws.on("connection", Connection); this.ws.on("connection", Connection);
} }
@ -38,6 +34,12 @@ export class Server {
await this.setupSchema(); await this.setupSchema();
await Config.init(); await Config.init();
console.log("[DB] connected"); console.log("[DB] connected");
console.log(`[Gateway] online on 0.0.0.0:${port}`); this.server.listen(this.port);
console.log(`[Gateway] online on 0.0.0.0:${this.port}`);
}
async stop() {
await db.close();
this.server.close();
} }
} }

View File

@ -1,5 +1,6 @@
import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdate.ts"; import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdate.ts";
import { CLOSECODES, Payload } from "../util/Constants"; import { CLOSECODES, Payload } from "../util/Constants";
import { Send } from "../util/Send";
import WebSocket from "../util/WebSocket"; import WebSocket from "../util/WebSocket";
import { check } from "./instanceOf"; import { check } from "./instanceOf";
@ -8,6 +9,18 @@ import { check } from "./instanceOf";
// TODO: save voice servers in database and retrieve them // TODO: save voice servers in database and retrieve them
// Notice: Bot users respect the voice channel's user limit, if set. When the voice channel is full, you will not receive the Voice State Update or Voice Server Update events in response to your own Voice State Update. Having MANAGE_CHANNELS permission bypasses this limit and allows you to join regardless of the channel being full or not. // Notice: Bot users respect the voice channel's user limit, if set. When the voice channel is full, you will not receive the Voice State Update or Voice Server Update events in response to your own Voice State Update. Having MANAGE_CHANNELS permission bypasses this limit and allows you to join regardless of the channel being full or not.
export function onVoiceStateUpdate(this: WebSocket, data: Payload) { export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
check.call(this, VoiceStateUpdateSchema, data.d); check.call(this, VoiceStateUpdateSchema, data.d);
const body = data.d as VoiceStateUpdateSchema;
await Send(this, {
op: 0,
s: this.sequence++,
t: "VOICE_SERVER_UPDATE",
d: {
token: ``,
guild_id: body.guild_id,
endpoint: `localhost:3004`,
},
});
} }

View File

@ -5,5 +5,10 @@ import { Server } from "./Server";
import { config } from "dotenv"; import { config } from "dotenv";
config(); config();
const server = new Server(); var port = Number(process.env.PORT);
if (isNaN(port)) port = 3002;
const server = new Server({
port,
});
server.listen(); server.listen();