@stonyx/sockets 0.1.1-beta.13 → 0.1.1-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.d.ts CHANGED
@@ -24,7 +24,7 @@ export default class SocketClient {
24
24
  resolve: () => void;
25
25
  reject: (reason?: unknown) => void;
26
26
  } | null;
27
- onDisconnect: (() => void) | null;
27
+ onDisconnect: ((code: number, reason: string) => void) | null;
28
28
  onReconnecting: ((attempt: number, delay: number) => void) | null;
29
29
  onReconnected: (() => void) | null;
30
30
  onReconnectFailed: (() => void) | null;
@@ -36,7 +36,7 @@ export default class SocketClient {
36
36
  send(payload: SocketMessage, useGlobalKey?: boolean): void;
37
37
  heartBeat(): void;
38
38
  nextHeartBeat(): void;
39
- onClose(): void;
39
+ onClose(code?: number, reason?: string): void;
40
40
  close(): void;
41
41
  getReconnectDelay(): number;
42
42
  reconnect(): Promise<void>;
package/dist/client.js CHANGED
@@ -55,7 +55,7 @@ export default class SocketClient {
55
55
  const socket = new WebSocket(address);
56
56
  this.socket = socket;
57
57
  socket.on('message', (data) => this.onMessage(data));
58
- socket.on('close', () => this.onClose());
58
+ socket.on('close', (code, reason) => this.onClose(code, reason.toString()));
59
59
  socket.on('error', () => {
60
60
  log.socket(`Error connecting to socket server`);
61
61
  reject('Error connecting to socket server');
@@ -118,11 +118,12 @@ export default class SocketClient {
118
118
  const { heartBeatInterval } = config.sockets;
119
119
  this._heartBeatTimer = setTimeout(() => this.heartBeat(), heartBeatInterval);
120
120
  }
121
- onClose() {
122
- log.socket('Disconnected from remote server');
121
+ // ws always provides code and reason; params are optional for direct calls and testing
122
+ onClose(code, reason) {
123
+ log.socket(`Disconnected from remote server (code: ${code ?? 'unknown'}, reason: ${reason || 'none'})`);
123
124
  if (this._heartBeatTimer)
124
125
  clearTimeout(this._heartBeatTimer);
125
- this.onDisconnect?.();
126
+ this.onDisconnect?.(code ?? 1006, reason ?? '');
126
127
  if (!this._intentionalClose) {
127
128
  this.reconnect();
128
129
  }
package/dist/server.d.ts CHANGED
@@ -31,14 +31,14 @@ export default class SocketServer {
31
31
  wss: WebSocketServer | null;
32
32
  encryptionEnabled: boolean;
33
33
  globalKey: Buffer | null;
34
- onClientDisconnect: ((client: ConnectedClient) => void) | null;
34
+ onClientDisconnect: ((client: ConnectedClient, code: number, reason: string) => void) | null;
35
35
  constructor();
36
36
  init(): Promise<void>;
37
37
  discoverHandlers(): Promise<void>;
38
38
  validateAuthHandler(): void;
39
39
  onMessage(payload: Buffer | string, client: ConnectedClient): Promise<void>;
40
40
  prepareSend(client: ConnectedClient, ws: WebSocket): void;
41
- handleDisconnect(client: ConnectedClient): void;
41
+ handleDisconnect(client: ConnectedClient, code?: number, reason?: string): void;
42
42
  sendTo(clientId: number, request: string, response: unknown): void;
43
43
  sendToByMeta(key: string, value: unknown, request: string, response: unknown): boolean;
44
44
  broadcast(request: string, response: unknown): void;
package/dist/server.js CHANGED
@@ -37,7 +37,7 @@ export default class SocketServer {
37
37
  client.__authenticated = false;
38
38
  this.prepareSend(client, ws);
39
39
  ws.on('message', (payload) => this.onMessage(payload, client));
40
- ws.on('close', () => this.handleDisconnect(client));
40
+ ws.on('close', (code, reason) => this.handleDisconnect(client, code, reason.toString()));
41
41
  });
42
42
  }
43
43
  async discoverHandlers() {
@@ -122,11 +122,12 @@ export default class SocketServer {
122
122
  }
123
123
  };
124
124
  }
125
- handleDisconnect(client) {
125
+ // ws always provides code and reason; params are optional for direct calls and testing
126
+ handleDisconnect(client, code, reason) {
126
127
  const { ip } = client;
127
- log.socket(`[${ip}] Client disconnected`);
128
+ log.socket(`[${ip}] Client disconnected (code: ${code ?? 'unknown'}, reason: ${reason || 'none'})`);
128
129
  this.clientMap.delete(client.id);
129
- this.onClientDisconnect?.(client);
130
+ this.onClientDisconnect?.(client, code ?? 1006, reason ?? '');
130
131
  }
131
132
  sendTo(clientId, request, response) {
132
133
  const client = this.clientMap.get(clientId);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.1.1-beta.13",
7
+ "version": "0.1.1-beta.14",
8
8
  "description": "WebSocket server and client module for the Stonyx framework",
9
9
  "main": "dist/main.js",
10
10
  "types": "dist/main.d.ts",