@midwayjs/socketio 3.11.0 → 3.11.2
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/framework.js +40 -25
- package/package.json +4 -4
package/dist/framework.js
CHANGED
|
@@ -80,41 +80,55 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
|
|
|
80
80
|
socket.app = this.app;
|
|
81
81
|
next();
|
|
82
82
|
});
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
//
|
|
86
|
-
|
|
83
|
+
// `connect`事件应该使用**同步**的方式调用以保证后续事件监听能够正常得到处理
|
|
84
|
+
nsp.on('connect', (socket) => {
|
|
85
|
+
// 异步执行全局和Controller级的连接中间件
|
|
86
|
+
let connMiddlewarePromise = this.middlewareService
|
|
87
|
+
.compose([
|
|
87
88
|
...this.connectionMiddlewareManager,
|
|
88
89
|
...controllerConnectionMiddleware,
|
|
89
|
-
], this.app)
|
|
90
|
-
|
|
90
|
+
], this.app)
|
|
91
|
+
.then(connectFn => connectFn(socket));
|
|
91
92
|
const wsEventInfos = (0, core_2.getClassMetadata)(core_2.WS_EVENT_KEY, target);
|
|
92
93
|
// 存储方法对应的响应处理
|
|
93
94
|
const methodMap = {};
|
|
94
|
-
|
|
95
|
-
|
|
95
|
+
// 优先处理`@OnWSConnection`
|
|
96
|
+
const wsOnConnectionEventInfos = wsEventInfos.filter(wsEventInfo => wsEventInfo.eventType === core_2.WSEventTypeEnum.ON_CONNECTION);
|
|
97
|
+
if (wsOnConnectionEventInfos.length) {
|
|
98
|
+
// `@OnWSConnection`应该在之前的连接中间件执行完成后继续
|
|
99
|
+
connMiddlewarePromise = connMiddlewarePromise.then(() =>
|
|
100
|
+
// 可能存在多个`@OnWSConnection`,这时不保障它们的执行顺序
|
|
101
|
+
Promise.all(wsOnConnectionEventInfos.map(async (wsEventInfo) => {
|
|
102
|
+
var _a;
|
|
96
103
|
methodMap[wsEventInfo.propertyName] = methodMap[wsEventInfo.propertyName] || { responseEvents: [] };
|
|
97
104
|
const controller = await socket.requestContext.getAsync(target);
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const result = await fn(socket);
|
|
109
|
-
await this.bindSocketResponse(result, socket, wsEventInfo.propertyName, methodMap);
|
|
110
|
-
}
|
|
111
|
-
catch (err) {
|
|
112
|
-
this.logger.error(err);
|
|
113
|
-
}
|
|
105
|
+
try {
|
|
106
|
+
const fn = await this.middlewareService.compose([
|
|
107
|
+
...(((_a = wsEventInfo === null || wsEventInfo === void 0 ? void 0 : wsEventInfo.eventOptions) === null || _a === void 0 ? void 0 : _a.middleware) || []),
|
|
108
|
+
async (ctx, next) => {
|
|
109
|
+
// eslint-disable-next-line prefer-spread
|
|
110
|
+
return controller[wsEventInfo.propertyName].apply(controller, [socket]);
|
|
111
|
+
},
|
|
112
|
+
], this.app);
|
|
113
|
+
const result = await fn(socket);
|
|
114
|
+
await this.bindSocketResponse(result, socket, wsEventInfo.propertyName, methodMap);
|
|
114
115
|
}
|
|
115
|
-
|
|
116
|
+
catch (err) {
|
|
117
|
+
this.logger.error(err);
|
|
118
|
+
}
|
|
119
|
+
})));
|
|
120
|
+
}
|
|
121
|
+
// 监听和处理其它非`@OnWSConnection`的逻辑
|
|
122
|
+
const wsRestEventInfos = wsEventInfos.filter(wsEventInfo => wsEventInfo.eventType !== core_2.WSEventTypeEnum.ON_CONNECTION);
|
|
123
|
+
if (wsRestEventInfos.length) {
|
|
124
|
+
for (const wsEventInfo of wsRestEventInfos) {
|
|
125
|
+
methodMap[wsEventInfo.propertyName] = methodMap[wsEventInfo.propertyName] || { responseEvents: [] };
|
|
126
|
+
if (wsEventInfo.eventType === core_2.WSEventTypeEnum.ON_MESSAGE) {
|
|
116
127
|
// on user custom event
|
|
117
128
|
socket.on(wsEventInfo.messageEventName, async (...args) => {
|
|
129
|
+
// 需要等待所有连接中间件和@OnWSConnection执行完成后再触发@OnWSMessage
|
|
130
|
+
await connMiddlewarePromise;
|
|
131
|
+
const controller = await socket.requestContext.getAsync(target);
|
|
118
132
|
debug('got message', wsEventInfo.messageEventName, args);
|
|
119
133
|
try {
|
|
120
134
|
const result = await (await this.applyMiddleware(async (ctx, next) => {
|
|
@@ -153,6 +167,7 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
|
|
|
153
167
|
else if (wsEventInfo.eventType === core_2.WSEventTypeEnum.ON_DISCONNECTION) {
|
|
154
168
|
// on socket disconnect
|
|
155
169
|
socket.on('disconnect', async (reason) => {
|
|
170
|
+
const controller = await socket.requestContext.getAsync(target);
|
|
156
171
|
try {
|
|
157
172
|
const result = await controller[wsEventInfo.propertyName].apply(controller, [reason]);
|
|
158
173
|
await this.bindSocketResponse(result, socket, wsEventInfo.propertyName, methodMap);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/socketio",
|
|
3
|
-
"version": "3.11.
|
|
3
|
+
"version": "3.11.2",
|
|
4
4
|
"description": "Midway Web Framework for socket.io",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
],
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@midwayjs/mock": "^3.11.
|
|
27
|
+
"@midwayjs/mock": "^3.11.1",
|
|
28
28
|
"fs-extra": "10.1.0",
|
|
29
29
|
"socket.io-client": "4.6.1"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@midwayjs/core": "^3.11.
|
|
32
|
+
"@midwayjs/core": "^3.11.1",
|
|
33
33
|
"@midwayjs/logger": "^2.15.0",
|
|
34
34
|
"socket.io": "4.6.1",
|
|
35
35
|
"socket.io-redis": "^6.1.0"
|
|
@@ -42,5 +42,5 @@
|
|
|
42
42
|
"engines": {
|
|
43
43
|
"node": ">=12"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "53e5a4f4a05553522a08334f825bfd4b2e7d7586"
|
|
46
46
|
}
|