Skip to content

Commit

Permalink
add ping pong
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed Sep 24, 2024
1 parent 99d45a5 commit 5cbefab
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 10 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/ConnectionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand All @@ -213,6 +217,7 @@ export async function routeRequest(
}
}
connections.clear();
clearTimeout(pingInterval);
});

// SEND the initial continue packet with streamID 0 and 127 queue limit
Expand Down
3 changes: 2 additions & 1 deletion src/Packets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export enum LOG_LEVEL {
}
export type WispOptions = {
logLevel: LOG_LEVEL;
pingInterval: number;
};
3 changes: 2 additions & 1 deletion src/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});

0 comments on commit 5cbefab

Please sign in to comment.