@nightlylabs/dex-sdk 0.3.39 → 0.3.40

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/index.cjs CHANGED
@@ -23936,6 +23936,8 @@ var getRandomId = () => (0, import_uuid.v7)();
23936
23936
  var Client = class _Client {
23937
23937
  constructor(connection, config = {}) {
23938
23938
  this._apiKeySequenceNumber = 0;
23939
+ this._pingIntervalMs = 3e4;
23940
+ this._lastPongAt = 0;
23939
23941
  this._subscriptions = /* @__PURE__ */ new Map();
23940
23942
  this._gasUnitPrice = 100;
23941
23943
  this._expirationTimestampDelay = 20;
@@ -24395,6 +24397,7 @@ var Client = class _Client {
24395
24397
  this._expirationTimestampDelay = config.params?.expirationTimestampSecs || 20;
24396
24398
  this.wsDebug = config.wsDebug || false;
24397
24399
  this.timeout = config.timeout || 5e3;
24400
+ this._pingIntervalMs = config.pingIntervalMs || 3e4;
24398
24401
  }
24399
24402
  static async init(connection, config = {}) {
24400
24403
  const id = config.chainId || await connection.getChainId();
@@ -24504,6 +24507,12 @@ var Client = class _Client {
24504
24507
  }
24505
24508
  _setupWebSocketHandlers() {
24506
24509
  if (!this._ws) return;
24510
+ this._lastPongAt = Date.now();
24511
+ this._pingInterval = setInterval(() => {
24512
+ if (this._ws && this._ws.readyState === WebSocket.OPEN) {
24513
+ this._ws.send(JSON.stringify({ type: "Ping" }));
24514
+ }
24515
+ }, this._pingIntervalMs);
24507
24516
  this._ws.onmessage = async (msg) => {
24508
24517
  let data;
24509
24518
  if (msg.data.arrayBuffer) {
@@ -24511,6 +24520,10 @@ var Client = class _Client {
24511
24520
  } else {
24512
24521
  data = msg.data;
24513
24522
  }
24523
+ if (data === '{"type":"Pong"}') {
24524
+ this._lastPongAt = Date.now();
24525
+ return;
24526
+ }
24514
24527
  const decodedMsg = JSON.parse(data);
24515
24528
  this._wsLog("Received:", decodedMsg.type, decodedMsg.content);
24516
24529
  const topic = getTopicFromMessage(decodedMsg);
@@ -24528,6 +24541,10 @@ var Client = class _Client {
24528
24541
  "clean=" + event.wasClean
24529
24542
  );
24530
24543
  this._ws = void 0;
24544
+ if (this._pingInterval) {
24545
+ clearInterval(this._pingInterval);
24546
+ this._pingInterval = void 0;
24547
+ }
24531
24548
  this._subscriptions.clear();
24532
24549
  this.onWsClose?.(event);
24533
24550
  };
@@ -24541,15 +24558,24 @@ var Client = class _Client {
24541
24558
  return;
24542
24559
  }
24543
24560
  this._wsLog("Disconnecting, clearing", this._subscriptions.size, "subscriptions");
24561
+ if (this._pingInterval) {
24562
+ clearInterval(this._pingInterval);
24563
+ this._pingInterval = void 0;
24564
+ }
24544
24565
  this._subscriptions.clear();
24545
24566
  this._ws.close();
24546
24567
  this._ws = void 0;
24547
24568
  }
24548
- // Method to check if WebSocket is connected
24569
+ // Method to check if WebSocket is connected and responsive
24549
24570
  isWebSocketConnected() {
24550
- const connected = this._ws !== void 0 && this._ws.readyState === WebSocket.OPEN;
24551
- this._wsLog("isWebSocketConnected:", connected);
24552
- return connected;
24571
+ if (!this._ws || this._ws.readyState !== WebSocket.OPEN) {
24572
+ this._wsLog("isWebSocketConnected: false (not open)");
24573
+ return false;
24574
+ }
24575
+ const pongTimeout = this._pingIntervalMs * 2;
24576
+ const alive = Date.now() - this._lastPongAt < pongTimeout;
24577
+ this._wsLog("isWebSocketConnected:", alive, "lastPong:", Date.now() - this._lastPongAt, "ms ago");
24578
+ return alive;
24553
24579
  }
24554
24580
  sendWsMessage(message) {
24555
24581
  if (!this._ws) throw new Error("WebSocket is not connected");
package/dist/index.d.cts CHANGED
@@ -5467,6 +5467,7 @@ interface ClientConfig {
5467
5467
  networkMode?: NetworkMode;
5468
5468
  wsDebug?: boolean;
5469
5469
  timeout?: number;
5470
+ pingIntervalMs?: number;
5470
5471
  }
5471
5472
  interface ChangePerpOrderParams {
5472
5473
  userId: string;
@@ -5777,6 +5778,9 @@ declare class Client {
5777
5778
  _apiKey: Account;
5778
5779
  _apiKeySequenceNumber: number;
5779
5780
  _ws: WebSocket | undefined;
5781
+ _pingInterval: ReturnType<typeof setInterval> | undefined;
5782
+ _pingIntervalMs: number;
5783
+ _lastPongAt: number;
5780
5784
  _serverUrl: string;
5781
5785
  _subscriptions: Map<string, (data: WsMessage) => void>;
5782
5786
  _sequenceNumberManager: AccountSequenceNumber;
package/dist/index.d.ts CHANGED
@@ -5467,6 +5467,7 @@ interface ClientConfig {
5467
5467
  networkMode?: NetworkMode;
5468
5468
  wsDebug?: boolean;
5469
5469
  timeout?: number;
5470
+ pingIntervalMs?: number;
5470
5471
  }
5471
5472
  interface ChangePerpOrderParams {
5472
5473
  userId: string;
@@ -5777,6 +5778,9 @@ declare class Client {
5777
5778
  _apiKey: Account;
5778
5779
  _apiKeySequenceNumber: number;
5779
5780
  _ws: WebSocket | undefined;
5781
+ _pingInterval: ReturnType<typeof setInterval> | undefined;
5782
+ _pingIntervalMs: number;
5783
+ _lastPongAt: number;
5780
5784
  _serverUrl: string;
5781
5785
  _subscriptions: Map<string, (data: WsMessage) => void>;
5782
5786
  _sequenceNumberManager: AccountSequenceNumber;
package/dist/index.js CHANGED
@@ -23894,6 +23894,8 @@ var getRandomId = () => uuidv7();
23894
23894
  var Client = class _Client {
23895
23895
  constructor(connection, config = {}) {
23896
23896
  this._apiKeySequenceNumber = 0;
23897
+ this._pingIntervalMs = 3e4;
23898
+ this._lastPongAt = 0;
23897
23899
  this._subscriptions = /* @__PURE__ */ new Map();
23898
23900
  this._gasUnitPrice = 100;
23899
23901
  this._expirationTimestampDelay = 20;
@@ -24353,6 +24355,7 @@ var Client = class _Client {
24353
24355
  this._expirationTimestampDelay = config.params?.expirationTimestampSecs || 20;
24354
24356
  this.wsDebug = config.wsDebug || false;
24355
24357
  this.timeout = config.timeout || 5e3;
24358
+ this._pingIntervalMs = config.pingIntervalMs || 3e4;
24356
24359
  }
24357
24360
  static async init(connection, config = {}) {
24358
24361
  const id = config.chainId || await connection.getChainId();
@@ -24462,6 +24465,12 @@ var Client = class _Client {
24462
24465
  }
24463
24466
  _setupWebSocketHandlers() {
24464
24467
  if (!this._ws) return;
24468
+ this._lastPongAt = Date.now();
24469
+ this._pingInterval = setInterval(() => {
24470
+ if (this._ws && this._ws.readyState === WebSocket.OPEN) {
24471
+ this._ws.send(JSON.stringify({ type: "Ping" }));
24472
+ }
24473
+ }, this._pingIntervalMs);
24465
24474
  this._ws.onmessage = async (msg) => {
24466
24475
  let data;
24467
24476
  if (msg.data.arrayBuffer) {
@@ -24469,6 +24478,10 @@ var Client = class _Client {
24469
24478
  } else {
24470
24479
  data = msg.data;
24471
24480
  }
24481
+ if (data === '{"type":"Pong"}') {
24482
+ this._lastPongAt = Date.now();
24483
+ return;
24484
+ }
24472
24485
  const decodedMsg = JSON.parse(data);
24473
24486
  this._wsLog("Received:", decodedMsg.type, decodedMsg.content);
24474
24487
  const topic = getTopicFromMessage(decodedMsg);
@@ -24486,6 +24499,10 @@ var Client = class _Client {
24486
24499
  "clean=" + event.wasClean
24487
24500
  );
24488
24501
  this._ws = void 0;
24502
+ if (this._pingInterval) {
24503
+ clearInterval(this._pingInterval);
24504
+ this._pingInterval = void 0;
24505
+ }
24489
24506
  this._subscriptions.clear();
24490
24507
  this.onWsClose?.(event);
24491
24508
  };
@@ -24499,15 +24516,24 @@ var Client = class _Client {
24499
24516
  return;
24500
24517
  }
24501
24518
  this._wsLog("Disconnecting, clearing", this._subscriptions.size, "subscriptions");
24519
+ if (this._pingInterval) {
24520
+ clearInterval(this._pingInterval);
24521
+ this._pingInterval = void 0;
24522
+ }
24502
24523
  this._subscriptions.clear();
24503
24524
  this._ws.close();
24504
24525
  this._ws = void 0;
24505
24526
  }
24506
- // Method to check if WebSocket is connected
24527
+ // Method to check if WebSocket is connected and responsive
24507
24528
  isWebSocketConnected() {
24508
- const connected = this._ws !== void 0 && this._ws.readyState === WebSocket.OPEN;
24509
- this._wsLog("isWebSocketConnected:", connected);
24510
- return connected;
24529
+ if (!this._ws || this._ws.readyState !== WebSocket.OPEN) {
24530
+ this._wsLog("isWebSocketConnected: false (not open)");
24531
+ return false;
24532
+ }
24533
+ const pongTimeout = this._pingIntervalMs * 2;
24534
+ const alive = Date.now() - this._lastPongAt < pongTimeout;
24535
+ this._wsLog("isWebSocketConnected:", alive, "lastPong:", Date.now() - this._lastPongAt, "ms ago");
24536
+ return alive;
24511
24537
  }
24512
24538
  sendWsMessage(message) {
24513
24539
  if (!this._ws) throw new Error("WebSocket is not connected");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nightlylabs/dex-sdk",
3
- "version": "0.3.39",
3
+ "version": "0.3.40",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {