@nocobase/server 1.5.0-beta.30 → 1.5.0-beta.31
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 +2 -0
- package/lib/gateway/ws-server.js +60 -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;
|
|
@@ -44,6 +45,7 @@ export declare class WSServer extends EventEmitter {
|
|
|
44
45
|
tagName: string;
|
|
45
46
|
tagValue: string;
|
|
46
47
|
}>, sendMessage: object): void;
|
|
48
|
+
sendToClient(clientId: string, sendMessage: object): void;
|
|
47
49
|
loopThroughConnections(callback: (client: WebSocketClient) => void): void;
|
|
48
50
|
close(): void;
|
|
49
51
|
}
|
package/lib/gateway/ws-server.js
CHANGED
|
@@ -133,6 +133,57 @@ 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:sendToClient", ({ clientId, message }) => {
|
|
176
|
+
this.sendToClient(clientId, message);
|
|
177
|
+
});
|
|
178
|
+
app.on("ws:sendToCurrentApp", ({ message }) => {
|
|
179
|
+
this.sendToConnectionsByTag("app", app.name, message);
|
|
180
|
+
});
|
|
181
|
+
app.on("ws:sendToTags", ({ tags, message }) => {
|
|
182
|
+
this.sendToConnectionsByTags(tags, message);
|
|
183
|
+
});
|
|
184
|
+
app.on("ws:authorized", ({ clientId, userId }) => {
|
|
185
|
+
this.sendToClient(clientId, { type: "authorized" });
|
|
186
|
+
});
|
|
136
187
|
}
|
|
137
188
|
addNewConnection(ws, request) {
|
|
138
189
|
const id = (0, import_nanoid.nanoid)();
|
|
@@ -149,6 +200,9 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
149
200
|
}
|
|
150
201
|
setClientTag(clientId, tagKey, tagValue) {
|
|
151
202
|
const client = this.webSocketClients.get(clientId);
|
|
203
|
+
if (!client) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
152
206
|
client.tags.add(`${tagKey}#${tagValue}`);
|
|
153
207
|
console.log(`client tags: ${Array.from(client.tags)}`);
|
|
154
208
|
}
|
|
@@ -197,6 +251,12 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
197
251
|
}
|
|
198
252
|
});
|
|
199
253
|
}
|
|
254
|
+
sendToClient(clientId, sendMessage) {
|
|
255
|
+
const client = this.webSocketClients.get(clientId);
|
|
256
|
+
if (client) {
|
|
257
|
+
this.sendMessageToConnection(client, sendMessage);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
200
260
|
loopThroughConnections(callback) {
|
|
201
261
|
this.webSocketClients.forEach((client) => {
|
|
202
262
|
callback(client);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/server",
|
|
3
|
-
"version": "1.5.0-beta.
|
|
3
|
+
"version": "1.5.0-beta.31",
|
|
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.5.0-beta.
|
|
14
|
-
"@nocobase/actions": "1.5.0-beta.
|
|
15
|
-
"@nocobase/auth": "1.5.0-beta.
|
|
16
|
-
"@nocobase/cache": "1.5.0-beta.
|
|
17
|
-
"@nocobase/data-source-manager": "1.5.0-beta.
|
|
18
|
-
"@nocobase/database": "1.5.0-beta.
|
|
19
|
-
"@nocobase/evaluators": "1.5.0-beta.
|
|
20
|
-
"@nocobase/lock-manager": "1.5.0-beta.
|
|
21
|
-
"@nocobase/logger": "1.5.0-beta.
|
|
22
|
-
"@nocobase/resourcer": "1.5.0-beta.
|
|
23
|
-
"@nocobase/sdk": "1.5.0-beta.
|
|
24
|
-
"@nocobase/telemetry": "1.5.0-beta.
|
|
25
|
-
"@nocobase/utils": "1.5.0-beta.
|
|
13
|
+
"@nocobase/acl": "1.5.0-beta.31",
|
|
14
|
+
"@nocobase/actions": "1.5.0-beta.31",
|
|
15
|
+
"@nocobase/auth": "1.5.0-beta.31",
|
|
16
|
+
"@nocobase/cache": "1.5.0-beta.31",
|
|
17
|
+
"@nocobase/data-source-manager": "1.5.0-beta.31",
|
|
18
|
+
"@nocobase/database": "1.5.0-beta.31",
|
|
19
|
+
"@nocobase/evaluators": "1.5.0-beta.31",
|
|
20
|
+
"@nocobase/lock-manager": "1.5.0-beta.31",
|
|
21
|
+
"@nocobase/logger": "1.5.0-beta.31",
|
|
22
|
+
"@nocobase/resourcer": "1.5.0-beta.31",
|
|
23
|
+
"@nocobase/sdk": "1.5.0-beta.31",
|
|
24
|
+
"@nocobase/telemetry": "1.5.0-beta.31",
|
|
25
|
+
"@nocobase/utils": "1.5.0-beta.31",
|
|
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": "75fe2500e19dfbb2ae1a29a27f136f3b794e633a"
|
|
60
60
|
}
|