openkbs-pulse 1.0.13 → 1.0.15

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.
Files changed (3) hide show
  1. package/package.json +2 -1
  2. package/pulse.js +21 -0
  3. package/server.js +0 -7
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "openkbs-pulse",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "Real-time WebSocket SDK for OpenKBS",
5
+ "type": "module",
5
6
  "main": "pulse.js",
6
7
  "types": "pulse.d.ts",
7
8
  "exports": {
package/pulse.js CHANGED
@@ -86,6 +86,7 @@
86
86
  */
87
87
  disconnect() {
88
88
  this._intentionalClose = true;
89
+ this._stopHeartbeat();
89
90
  this._setConnectionState('disconnecting');
90
91
 
91
92
  if (this._ws) {
@@ -140,6 +141,9 @@
140
141
  this._reconnectAttempt = 0;
141
142
  this._setConnectionState('connected');
142
143
 
144
+ // Start heartbeat to keep connection alive
145
+ this._startHeartbeat();
146
+
143
147
  // Resubscribe to all channels
144
148
  Object.values(this._channels).forEach(channel => {
145
149
  channel._resubscribe();
@@ -151,6 +155,7 @@
151
155
 
152
156
  this._ws.onclose = (e) => {
153
157
  this._log('Disconnected:', e.code, e.reason);
158
+ this._stopHeartbeat();
154
159
  this._setConnectionState('disconnected');
155
160
 
156
161
  if (!this._intentionalClose) {
@@ -234,6 +239,22 @@
234
239
  }, delay);
235
240
  }
236
241
 
242
+ _startHeartbeat() {
243
+ this._stopHeartbeat();
244
+ this._heartbeatInterval = setInterval(() => {
245
+ if (this._ws && this._ws.readyState === WebSocket.OPEN) {
246
+ this._send({ action: 'ping' });
247
+ }
248
+ }, 25000); // Ping every 25 seconds
249
+ }
250
+
251
+ _stopHeartbeat() {
252
+ if (this._heartbeatInterval) {
253
+ clearInterval(this._heartbeatInterval);
254
+ this._heartbeatInterval = null;
255
+ }
256
+ }
257
+
237
258
  _setConnectionState(state) {
238
259
  if (this._connectionState !== state) {
239
260
  this._connectionState = state;
package/server.js CHANGED
@@ -107,11 +107,4 @@ const pulse = {
107
107
  }
108
108
  };
109
109
 
110
- // ESM export
111
110
  export default pulse;
112
-
113
- // CommonJS compatibility
114
- if (typeof module !== 'undefined') {
115
- module.exports = pulse;
116
- module.exports.default = pulse;
117
- }