From 5cbefabcff0279196025ae8cc301596e8efa34bd Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Mon, 23 Sep 2024 20:34:19 -0500 Subject: [PATCH] add ping pong --- package.json | 4 ++-- pnpm-lock.yaml | 20 ++++++++++---------- src/ConnectionHandler.ts | 11 ++++++++--- src/Packets.ts | 3 ++- src/Types.ts | 1 + src/createServer.ts | 3 ++- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index dd06cc7..192b7f3 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,8 @@ "homepage": "https://github.com/MercuryWorkshop/wisp-server-node#readme", "license": "AGPL-3.0-only", "devDependencies": { - "@types/ws": "^8.5.11", - "typescript": "^5.3.3" + "@types/ws": "^8.5.12", + "typescript": "^5.6.2" }, "dependencies": { "bufferutil": "^4.0.8", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0c7ec90..05e0812 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -19,19 +19,19 @@ importers: version: 8.18.0(bufferutil@4.0.8)(utf-8-validate@6.0.4) devDependencies: '@types/ws': - specifier: ^8.5.11 - version: 8.5.11 + specifier: ^8.5.12 + version: 8.5.12 typescript: - specifier: ^5.3.3 - version: 5.5.3 + specifier: ^5.6.2 + version: 5.6.2 packages: '@types/node@20.14.10': resolution: {integrity: sha512-MdiXf+nDuMvY0gJKxyfZ7/6UFsETO7mGKF54MVD/ekJS6HdFtpZFBgrh6Pseu64XTb2MLyFPlbW6hj8HYRQNOQ==} - '@types/ws@8.5.11': - resolution: {integrity: sha512-4+q7P5h3SpJxaBft0Dzpbr6lmMaqh0Jr2tbhJZ/luAwvD7ohSCniYkwz/pLxuT2h0EOa6QADgJj1Ko+TzRfZ+w==} + '@types/ws@8.5.12': + resolution: {integrity: sha512-3tPRkv1EtkDpzlgyKyI8pGsGZAGPEaXeu0DOj5DI25Ja91bdAYddYHbADRYVrZMRbfW+1l5YwXVDKohDJNQxkQ==} bufferutil@4.0.8: resolution: {integrity: sha512-4T53u4PdgsXqKaIctwF8ifXlRTTmEPJ8iEPWFdGZvcf7sbwYo6FKFEX9eNNAnzFZ7EzJAQ3CJeOtCRA4rDp7Pw==} @@ -41,8 +41,8 @@ packages: resolution: {integrity: sha512-OSs33Z9yWr148JZcbZd5WiAXhh/n9z8TxQcdMhIOlpN9AhWpLfvVFO73+m77bBABQMaY9XSvIa+qk0jlI7Gcaw==} hasBin: true - typescript@5.5.3: - resolution: {integrity: sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==} + typescript@5.6.2: + resolution: {integrity: sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==} engines: {node: '>=14.17'} hasBin: true @@ -71,7 +71,7 @@ snapshots: dependencies: undici-types: 5.26.5 - '@types/ws@8.5.11': + '@types/ws@8.5.12': dependencies: '@types/node': 20.14.10 @@ -81,7 +81,7 @@ snapshots: node-gyp-build@4.8.1: {} - typescript@5.5.3: {} + typescript@5.6.2: {} undici-types@5.26.5: {} diff --git a/src/ConnectionHandler.ts b/src/ConnectionHandler.ts index 578d26d..66d5794 100644 --- a/src/ConnectionHandler.ts +++ b/src/ConnectionHandler.ts @@ -10,7 +10,7 @@ import { handleWsProxy } from "./wsproxy.ts"; import { checkErrorCode } from "./utils/Error.ts"; const wss = new WebSocket.Server({ noServer: true }); -const defaultOptions: WispOptions = { logLevel: LOG_LEVEL.INFO }; +const defaultOptions: WispOptions = { logLevel: LOG_LEVEL.INFO, pingInterval: 30 }; // Accepts either routeRequest(ws) or routeRequest(request, socket, head) like bare export async function routeRequest( wsOrIncomingMessage: WebSocket | IncomingMessage, @@ -40,8 +40,12 @@ export async function routeRequest( const connections = new Map(); const logger = new Logger(options.logLevel); + const pingInterval = setInterval(() => { + logger.debug(`sending websocket ping`); + ws.ping(); + }, options.pingInterval * 1000); - ws.on("message", async (data, isBinary) => { + ws.on("message", async (data: any) => { try { // Ensure that the incoming data is a valid WebSocket message if (!Buffer.isBuffer(data) && !(data instanceof ArrayBuffer)) { @@ -203,7 +207,7 @@ export async function routeRequest( }); // Close all open sockets when the WebSocket connection is closed - ws.on("close", (code, reason) => { + ws.on("close", (code: number, reason: string) => { logger.debug(`WebSocket connection closed with code ${code} and reason: ${reason}`); for (const { client } of connections.values()) { if (client instanceof net.Socket) { @@ -213,6 +217,7 @@ export async function routeRequest( } } connections.clear(); + clearTimeout(pingInterval); }); // SEND the initial continue packet with streamID 0 and 127 queue limit diff --git a/src/Packets.ts b/src/Packets.ts index 33fb6ed..73638cc 100644 --- a/src/Packets.ts +++ b/src/Packets.ts @@ -41,7 +41,8 @@ export function closePacketMaker(wispFrame: WispFrame, reason: number) { return closePacket.buffer; } -export function dataPacketMaker(wispFrame: WispFrame, data: Buffer) { +// the data is any because i want to build this shit without typescript screaming at me +export function dataPacketMaker(wispFrame: WispFrame, data: any) { // Only function here that returns a node buffer instead ArrayBufferLike // Packet header creation const dataPacketHeader = new DataView(new Uint8Array(5).buffer); diff --git a/src/Types.ts b/src/Types.ts index 9f0caaa..b8141d9 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -23,4 +23,5 @@ export enum LOG_LEVEL { } export type WispOptions = { logLevel: LOG_LEVEL; + pingInterval: number; }; diff --git a/src/createServer.ts b/src/createServer.ts index 3839391..7096f7e 100644 --- a/src/createServer.ts +++ b/src/createServer.ts @@ -8,6 +8,7 @@ const httpServer = http.createServer().listen(process.env.PORT || 3000); httpServer.on("upgrade", (req, socket, head) => { wisp.routeRequest(req, socket as Socket, head, { - logLevel: LOG_LEVEL.DEBUG + logLevel: LOG_LEVEL.DEBUG, + pingInterval: 30 }); });