@midwayjs/ws 3.14.4 → 3.14.9

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.
@@ -16,7 +16,10 @@ WebSocketConfiguration = __decorate([
16
16
  importConfigs: [
17
17
  {
18
18
  default: {
19
- webSocket: {},
19
+ webSocket: {
20
+ enableServerHeartbeatCheck: false,
21
+ serverHeartbeatInterval: 30000,
22
+ },
20
23
  },
21
24
  },
22
25
  ],
@@ -1,9 +1,11 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { BaseFramework, CommonMiddlewareUnion, ContextMiddlewareManager, IMidwayBootstrapOptions, MidwayFrameworkType } from '@midwayjs/core';
3
4
  import * as http from 'http';
4
5
  import { Application, Context, IMidwayWSApplication, IMidwayWSConfigurationOptions, NextFunction } from './interface';
5
6
  export declare class MidwayWSFramework extends BaseFramework<Application, Context, IMidwayWSConfigurationOptions> {
6
7
  server: http.Server;
8
+ protected heartBeatInterval: NodeJS.Timeout;
7
9
  protected connectionMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
8
10
  configure(): IMidwayWSConfigurationOptions;
9
11
  applicationInitialize(options: IMidwayBootstrapOptions): void;
@@ -18,5 +20,6 @@ export declare class MidwayWSFramework extends BaseFramework<Application, Contex
18
20
  getFrameworkName(): string;
19
21
  useConnectionMiddleware(middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>): void;
20
22
  getConnectionMiddleware(): ContextMiddlewareManager<Context, NextFunction, undefined>;
23
+ startHeartBeat(): void;
21
24
  }
22
25
  //# sourceMappingURL=framework.d.ts.map
package/dist/framework.js CHANGED
@@ -56,6 +56,9 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
56
56
  await new Promise(resolve => {
57
57
  server.listen(this.configurationOptions.port, () => {
58
58
  this.logger.info(`[midway:ws] WebSocket server port = ${this.configurationOptions.port} start success.`);
59
+ if (this.configurationOptions.enableServerHeartbeatCheck) {
60
+ this.startHeartBeat();
61
+ }
59
62
  resolve();
60
63
  });
61
64
  });
@@ -89,6 +92,13 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
89
92
  const controllerConnectionMiddleware = (_b = controllerOption.routerOptions.connectionMiddleware) !== null && _b !== void 0 ? _b : [];
90
93
  this.app.on('connection', async (socket, request) => {
91
94
  var _a;
95
+ socket.isAlive = true;
96
+ socket.on('error', error => {
97
+ this.logger.error(`socket got error: ${error}`);
98
+ });
99
+ socket.on('pong', () => {
100
+ socket.isAlive = true;
101
+ });
92
102
  // create request context
93
103
  this.app.createAnonymousContext(socket);
94
104
  socket.requestContext.registerObject('socket', socket);
@@ -181,9 +191,12 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
181
191
  }
182
192
  });
183
193
  this.app.on('error', err => {
184
- this.logger.error('socket server close', err);
194
+ this.logger.error('socket server got error', err);
185
195
  });
186
196
  this.app.on('close', () => {
197
+ if (this.heartBeatInterval) {
198
+ clearInterval(this.heartBeatInterval);
199
+ }
187
200
  this.logger.info('socket server close');
188
201
  });
189
202
  }
@@ -222,6 +235,18 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
222
235
  getConnectionMiddleware() {
223
236
  return this.connectionMiddlewareManager;
224
237
  }
238
+ startHeartBeat() {
239
+ this.heartBeatInterval = setInterval(() => {
240
+ this.app.clients.forEach((socket) => {
241
+ if (socket.isAlive === false) {
242
+ debug('[ws]: socket terminate');
243
+ return socket.terminate();
244
+ }
245
+ socket.isAlive = false;
246
+ socket.ping();
247
+ });
248
+ }, this.configurationOptions.serverHeartbeatInterval);
249
+ }
225
250
  };
226
251
  MidwayWSFramework = __decorate([
227
252
  (0, core_1.Framework)()
@@ -7,9 +7,18 @@ export type IMidwayWSApplication = IMidwayApplication<IMidwayWSContext, {
7
7
  export type IMidwayWSConfigurationOptions = {
8
8
  pubClient?: any;
9
9
  subClient?: any;
10
+ /**
11
+ * enable server heartbeat check, default is false
12
+ */
13
+ enableServerHeartbeatCheck?: boolean;
14
+ /**
15
+ * server heartbeat interval, default is 30000ms
16
+ */
17
+ serverHeartbeatInterval?: number;
10
18
  } & Partial<WebSocket.ServerOptions> & IConfigurationOptions;
11
19
  export type IMidwayWSContext = IMidwayContext<WebSocket & {
12
20
  app: IMidwayWSApplication;
21
+ isAlive: boolean;
13
22
  }>;
14
23
  export type Application = IMidwayWSApplication;
15
24
  export type NextFunction = BaseNextFunction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/ws",
3
- "version": "3.14.4",
3
+ "version": "3.14.9",
4
4
  "description": "Midway Web Framework for ws",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -23,12 +23,12 @@
23
23
  ],
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@midwayjs/mock": "^3.14.4",
27
- "@types/ws": "8.5.10",
26
+ "@midwayjs/core": "^3.14.4",
27
+ "@midwayjs/mock": "^3.14.7",
28
28
  "fs-extra": "11.2.0"
29
29
  },
30
30
  "dependencies": {
31
- "@midwayjs/core": "^3.14.4",
31
+ "@types/ws": "8.5.10",
32
32
  "ws": "8.16.0"
33
33
  },
34
34
  "author": "Harry Chen <czy88840616@gmail.com>",
@@ -39,5 +39,5 @@
39
39
  "engines": {
40
40
  "node": ">=12"
41
41
  },
42
- "gitHead": "72aacdfd87ef730f100690557de48dcbd4d806a7"
42
+ "gitHead": "dae4c9da12af95f6f632c86d4166fd2fa0067ecd"
43
43
  }