@stonyx/sockets 0.1.1-beta.12 → 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
@@ -45,6 +45,9 @@ export default class SocketClient {
45
45
  }, { ignoreAccessFailure: true });
46
46
  }
47
47
  async connect() {
48
+ if (this.sessionKey)
49
+ log.socket('Clearing stale sessionKey');
50
+ this.sessionKey = null;
48
51
  return new Promise((resolve, reject) => {
49
52
  const { address, authKey, authData } = config.sockets;
50
53
  this.promise = { resolve, reject };
@@ -52,7 +55,7 @@ export default class SocketClient {
52
55
  const socket = new WebSocket(address);
53
56
  this.socket = socket;
54
57
  socket.on('message', (data) => this.onMessage(data));
55
- socket.on('close', () => this.onClose());
58
+ socket.on('close', (code, reason) => this.onClose(code, reason.toString()));
56
59
  socket.on('error', () => {
57
60
  log.socket(`Error connecting to socket server`);
58
61
  reject('Error connecting to socket server');
@@ -115,11 +118,12 @@ export default class SocketClient {
115
118
  const { heartBeatInterval } = config.sockets;
116
119
  this._heartBeatTimer = setTimeout(() => this.heartBeat(), heartBeatInterval);
117
120
  }
118
- onClose() {
119
- 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'})`);
120
124
  if (this._heartBeatTimer)
121
125
  clearTimeout(this._heartBeatTimer);
122
- this.onDisconnect?.();
126
+ this.onDisconnect?.(code ?? 1006, reason ?? '');
123
127
  if (!this._intentionalClose) {
124
128
  this.reconnect();
125
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.12",
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",