diff --git a/packages/keepalive-ws/src/client/client.ts b/packages/keepalive-ws/src/client/client.ts index bb2d387..2b0acf2 100644 --- a/packages/keepalive-ws/src/client/client.ts +++ b/packages/keepalive-ws/src/client/client.ts @@ -131,10 +131,8 @@ export class KeepAliveClient extends EventEmitter { return new Promise((resolve, reject) => { try { - // Create a new WebSocket connection this.socket = new WebSocket(this.url); - // Set up a direct onopen handler to ensure we catch the connection event this.socket.onopen = () => { this._status = Status.ONLINE; this.connection.socket = this.socket; @@ -146,7 +144,6 @@ export class KeepAliveClient extends EventEmitter { resolve(); }; - // Set up a direct onerror handler for immediate connection errors this.socket.onerror = (error) => { this._status = Status.OFFLINE; reject( diff --git a/packages/keepalive-ws/src/server/command.ts b/packages/keepalive-ws/src/server/command.ts deleted file mode 100644 index e69de29..0000000 diff --git a/packages/keepalive-ws/src/server/connection.ts b/packages/keepalive-ws/src/server/connection.ts index 8938d2a..81d9af2 100644 --- a/packages/keepalive-ws/src/server/connection.ts +++ b/packages/keepalive-ws/src/server/connection.ts @@ -100,7 +100,7 @@ export class Connection extends EventEmitter { send(cmd: Command): boolean { if (this.isDead) return false; - + try { this.socket.send(stringifyCommand(cmd)); return true; @@ -112,7 +112,7 @@ export class Connection extends EventEmitter { close(): boolean { if (this.isDead) return false; - + try { this.socket.close(); return true; diff --git a/packages/keepalive-ws/tests/advanced.test.ts b/packages/keepalive-ws/tests/advanced.test.ts index 6d563fa..dc1172a 100644 --- a/packages/keepalive-ws/tests/advanced.test.ts +++ b/packages/keepalive-ws/tests/advanced.test.ts @@ -2,12 +2,11 @@ import { describe, test, expect, beforeEach, afterEach } from "vitest"; import { KeepAliveClient, Status } from "../src/client/client"; import { KeepAliveServer } from "../src/server/index"; -// Helper to create a WebSocket server for testing const createTestServer = (port: number) => { return new KeepAliveServer({ port, - pingInterval: 1000, // Faster for testing - latencyInterval: 500, // Faster for testing + pingInterval: 1000, + latencyInterval: 500, }); }; @@ -19,7 +18,6 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => { beforeEach(async () => { server = createTestServer(port); - // Wait for the server to start await new Promise((resolve) => { server.on("listening", () => { resolve(); @@ -35,12 +33,10 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => { }); afterEach(async () => { - // Close connections in order if (client.status === Status.ONLINE) { await client.close(); } - // Close the server return new Promise((resolve) => { if (server) { server.close(() => { @@ -59,7 +55,6 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => { await client.connect(); - // Expect it to fail after a short timeout await expect( client.command("never-responds", "Should timeout", 500), ).rejects.toThrow(/timed out/); @@ -72,13 +67,11 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => { await client.connect(); - // Expect to receive this error const result = await client.command("throws-error", "Will error", 1000); expect(result).toHaveProperty("error", "Custom server error"); }, 2000); test("multiple concurrent commands are handled correctly", async () => { - // Register commands with different delays await server.registerCommand("fast", async (context) => { await new Promise((r) => setTimeout(r, 50)); return `Fast: ${context.payload}`; @@ -95,14 +88,12 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => { await client.connect(); - // Send multiple commands concurrently const results = await Promise.all([ client.command("fast", "First", 1000), client.command("slow", "Second", 1000), client.command("echo", "Third", 1000), ]); - // Verify all commands completed successfully expect(results).toEqual(["Fast: First", "Slow: Second", "Echo: Third"]); }, 3000); @@ -128,7 +119,6 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => { const result = await client.command("echo", largeData, 5000); - // Verify the response contains the expected data expect(result).toEqual(largeData); }, 10000); @@ -137,25 +127,20 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => { return `Echo: ${context.payload}`; }); - // Create multiple clients const clients = Array(5) .fill(0) .map(() => new KeepAliveClient(`ws://localhost:${port}`)); - // Connect all clients await Promise.all(clients.map((client) => client.connect())); - // Send a command from each client const results = await Promise.all( clients.map((client, i) => client.command("echo", `Client ${i}`, 1000)), ); - // Verify all commands succeeded results.forEach((result, i) => { expect(result).toBe(`Echo: Client ${i}`); }); - // Clean up await Promise.all(clients.map((client) => client.close())); }, 5000); }); diff --git a/packages/keepalive-ws/tests/basic.test.ts b/packages/keepalive-ws/tests/basic.test.ts index b5810f5..b1ea141 100644 --- a/packages/keepalive-ws/tests/basic.test.ts +++ b/packages/keepalive-ws/tests/basic.test.ts @@ -1,14 +1,12 @@ import { describe, test, expect, beforeEach, afterEach } from "vitest"; import { KeepAliveClient, Status } from "../src/client/client"; import { KeepAliveServer } from "../src/server/index"; -import { WebSocket, WebSocketServer } from "ws"; -// Helper to create a WebSocket server for testing const createTestServer = (port: number) => { return new KeepAliveServer({ port, - pingInterval: 1000, // Faster for testing - latencyInterval: 500, // Faster for testing + pingInterval: 1000, + latencyInterval: 500, }); }; @@ -20,13 +18,11 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => { beforeEach(async () => { server = createTestServer(port); - // Wait for the server to start await new Promise((resolve) => { server.on("listening", () => { resolve(); }); - // In case the server is already listening if (server.listening) { resolve(); } @@ -36,12 +32,10 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => { }); afterEach(async () => { - // Close connections in order if (client.status === Status.ONLINE) { await client.close(); } - // Close the server return new Promise((resolve) => { if (server) { server.close(() => { @@ -81,7 +75,6 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => { await client.connect(); expect(client.status).toBe(Status.ONLINE); - // Second connect should resolve immediately await client.connect(); expect(client.status).toBe(Status.ONLINE); }, 10000); @@ -90,7 +83,6 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => { await client.close(); expect(client.status).toBe(Status.OFFLINE); - // Second close should resolve immediately await client.close(); expect(client.status).toBe(Status.OFFLINE); }, 10000);