@nocobase/server 1.6.0-alpha.12 → 1.6.0-alpha.13
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/lib/gateway/index.js +0 -45
- package/lib/gateway/ws-server.d.ts +1 -0
- package/lib/gateway/ws-server.js +57 -0
- package/package.json +15 -15
package/lib/gateway/index.js
CHANGED
|
@@ -355,51 +355,6 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
|
|
|
355
355
|
}
|
|
356
356
|
this.server = import_http.default.createServer(this.getCallback());
|
|
357
357
|
this.wsServer = new import_ws_server.WSServer();
|
|
358
|
-
this.wsServer.on("message", async ({ client, message }) => {
|
|
359
|
-
const app = await import_app_supervisor.AppSupervisor.getInstance().getApp(client.app);
|
|
360
|
-
if (!app) {
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
const parsedMessage = JSON.parse(message.toString());
|
|
364
|
-
if (!parsedMessage.type) {
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
if (!app.listenerCount(`ws:setTag`)) {
|
|
368
|
-
app.on("ws:setTag", ({ clientId, tagKey, tagValue }) => {
|
|
369
|
-
this.wsServer.setClientTag(clientId, tagKey, tagValue);
|
|
370
|
-
});
|
|
371
|
-
app.on("ws:removeTag", ({ clientId, tagKey }) => {
|
|
372
|
-
this.wsServer.removeClientTag(clientId, tagKey);
|
|
373
|
-
});
|
|
374
|
-
app.on("ws:sendToTag", ({ tagKey, tagValue, message: message2 }) => {
|
|
375
|
-
this.wsServer.sendToConnectionsByTags(
|
|
376
|
-
[
|
|
377
|
-
{ tagName: tagKey, tagValue },
|
|
378
|
-
{ tagName: "app", tagValue: app.name }
|
|
379
|
-
],
|
|
380
|
-
message2
|
|
381
|
-
);
|
|
382
|
-
});
|
|
383
|
-
app.on("ws:sendToTags", ({ tags, message: message2 }) => {
|
|
384
|
-
this.wsServer.sendToConnectionsByTags(tags, message2);
|
|
385
|
-
});
|
|
386
|
-
app.on("ws:authorized", ({ clientId, userId }) => {
|
|
387
|
-
this.wsServer.sendToConnectionsByTags(
|
|
388
|
-
[
|
|
389
|
-
{ tagName: "userId", tagValue: userId },
|
|
390
|
-
{ tagName: "app", tagValue: app.name }
|
|
391
|
-
],
|
|
392
|
-
{ type: "authorized" }
|
|
393
|
-
);
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
const eventName = `ws:message:${parsedMessage.type}`;
|
|
397
|
-
app.emit(eventName, {
|
|
398
|
-
clientId: client.id,
|
|
399
|
-
tags: [...client.tags],
|
|
400
|
-
payload: parsedMessage.payload
|
|
401
|
-
});
|
|
402
|
-
});
|
|
403
358
|
this.server.on("upgrade", (request, socket, head) => {
|
|
404
359
|
const { pathname } = (0, import_url.parse)(request.url);
|
|
405
360
|
if (pathname === import_node_process.default.env.WS_PATH) {
|
|
@@ -28,6 +28,7 @@ export declare class WSServer extends EventEmitter {
|
|
|
28
28
|
webSocketClients: Map<string, WebSocketClient>;
|
|
29
29
|
logger: Logger;
|
|
30
30
|
constructor();
|
|
31
|
+
bindAppWSEvents(app: any): void;
|
|
31
32
|
addNewConnection(ws: WebSocketWithId, request: IncomingMessage): WebSocketClient;
|
|
32
33
|
setClientTag(clientId: string, tagKey: string, tagValue: string): void;
|
|
33
34
|
removeClientTag(clientId: string, tagKey: string): void;
|
package/lib/gateway/ws-server.js
CHANGED
|
@@ -133,6 +133,60 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
133
133
|
}
|
|
134
134
|
});
|
|
135
135
|
});
|
|
136
|
+
import_app_supervisor.AppSupervisor.getInstance().on("afterAppAdded", (app) => {
|
|
137
|
+
this.bindAppWSEvents(app);
|
|
138
|
+
});
|
|
139
|
+
this.on("message", async ({ client, message }) => {
|
|
140
|
+
const app = await import_app_supervisor.AppSupervisor.getInstance().getApp(client.app);
|
|
141
|
+
if (!app) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const parsedMessage = JSON.parse(message.toString());
|
|
145
|
+
if (!parsedMessage.type) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const eventName = `ws:message:${parsedMessage.type}`;
|
|
149
|
+
app.emit(eventName, {
|
|
150
|
+
clientId: client.id,
|
|
151
|
+
tags: [...client.tags],
|
|
152
|
+
payload: parsedMessage.payload
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
bindAppWSEvents(app) {
|
|
157
|
+
if (app.listenerCount("ws:setTag") > 0) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
app.on("ws:setTag", ({ clientId, tagKey, tagValue }) => {
|
|
161
|
+
this.setClientTag(clientId, tagKey, tagValue);
|
|
162
|
+
});
|
|
163
|
+
app.on("ws:removeTag", ({ clientId, tagKey }) => {
|
|
164
|
+
this.removeClientTag(clientId, tagKey);
|
|
165
|
+
});
|
|
166
|
+
app.on("ws:sendToTag", ({ tagKey, tagValue, message }) => {
|
|
167
|
+
this.sendToConnectionsByTags(
|
|
168
|
+
[
|
|
169
|
+
{ tagName: tagKey, tagValue },
|
|
170
|
+
{ tagName: "app", tagValue: app.name }
|
|
171
|
+
],
|
|
172
|
+
message
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
app.on("ws:sendToCurrentApp", ({ message }) => {
|
|
176
|
+
this.sendToConnectionsByTag("app", app.name, message);
|
|
177
|
+
});
|
|
178
|
+
app.on("ws:sendToTags", ({ tags, message }) => {
|
|
179
|
+
this.sendToConnectionsByTags(tags, message);
|
|
180
|
+
});
|
|
181
|
+
app.on("ws:authorized", ({ clientId, userId }) => {
|
|
182
|
+
this.sendToConnectionsByTags(
|
|
183
|
+
[
|
|
184
|
+
{ tagName: "userId", tagValue: userId },
|
|
185
|
+
{ tagName: "app", tagValue: app.name }
|
|
186
|
+
],
|
|
187
|
+
{ type: "authorized" }
|
|
188
|
+
);
|
|
189
|
+
});
|
|
136
190
|
}
|
|
137
191
|
addNewConnection(ws, request) {
|
|
138
192
|
const id = (0, import_nanoid.nanoid)();
|
|
@@ -149,6 +203,9 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
149
203
|
}
|
|
150
204
|
setClientTag(clientId, tagKey, tagValue) {
|
|
151
205
|
const client = this.webSocketClients.get(clientId);
|
|
206
|
+
if (!client) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
152
209
|
client.tags.add(`${tagKey}#${tagValue}`);
|
|
153
210
|
console.log(`client tags: ${Array.from(client.tags)}`);
|
|
154
211
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/server",
|
|
3
|
-
"version": "1.6.0-alpha.
|
|
3
|
+
"version": "1.6.0-alpha.13",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -10,19 +10,19 @@
|
|
|
10
10
|
"@koa/cors": "^3.1.0",
|
|
11
11
|
"@koa/multer": "^3.0.2",
|
|
12
12
|
"@koa/router": "^9.4.0",
|
|
13
|
-
"@nocobase/acl": "1.6.0-alpha.
|
|
14
|
-
"@nocobase/actions": "1.6.0-alpha.
|
|
15
|
-
"@nocobase/auth": "1.6.0-alpha.
|
|
16
|
-
"@nocobase/cache": "1.6.0-alpha.
|
|
17
|
-
"@nocobase/data-source-manager": "1.6.0-alpha.
|
|
18
|
-
"@nocobase/database": "1.6.0-alpha.
|
|
19
|
-
"@nocobase/evaluators": "1.6.0-alpha.
|
|
20
|
-
"@nocobase/lock-manager": "1.6.0-alpha.
|
|
21
|
-
"@nocobase/logger": "1.6.0-alpha.
|
|
22
|
-
"@nocobase/resourcer": "1.6.0-alpha.
|
|
23
|
-
"@nocobase/sdk": "1.6.0-alpha.
|
|
24
|
-
"@nocobase/telemetry": "1.6.0-alpha.
|
|
25
|
-
"@nocobase/utils": "1.6.0-alpha.
|
|
13
|
+
"@nocobase/acl": "1.6.0-alpha.13",
|
|
14
|
+
"@nocobase/actions": "1.6.0-alpha.13",
|
|
15
|
+
"@nocobase/auth": "1.6.0-alpha.13",
|
|
16
|
+
"@nocobase/cache": "1.6.0-alpha.13",
|
|
17
|
+
"@nocobase/data-source-manager": "1.6.0-alpha.13",
|
|
18
|
+
"@nocobase/database": "1.6.0-alpha.13",
|
|
19
|
+
"@nocobase/evaluators": "1.6.0-alpha.13",
|
|
20
|
+
"@nocobase/lock-manager": "1.6.0-alpha.13",
|
|
21
|
+
"@nocobase/logger": "1.6.0-alpha.13",
|
|
22
|
+
"@nocobase/resourcer": "1.6.0-alpha.13",
|
|
23
|
+
"@nocobase/sdk": "1.6.0-alpha.13",
|
|
24
|
+
"@nocobase/telemetry": "1.6.0-alpha.13",
|
|
25
|
+
"@nocobase/utils": "1.6.0-alpha.13",
|
|
26
26
|
"@types/decompress": "4.2.7",
|
|
27
27
|
"@types/ini": "^1.3.31",
|
|
28
28
|
"@types/koa-send": "^4.1.3",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"@types/serve-handler": "^6.1.1",
|
|
57
57
|
"@types/ws": "^8.5.5"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "b051b9016899d0c9fd5b54543d3898edace9950b"
|
|
60
60
|
}
|