mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
chore: ws api
This commit is contained in:
parent
6f700d53fb
commit
9e93c30c15
@ -433,62 +433,6 @@ export class Gateway extends EventEmitter {
|
||||
|
||||
this.wsServer = new WSServer();
|
||||
|
||||
this.wsServer.on('message', async ({ client, message }) => {
|
||||
const app = await AppSupervisor.getInstance().getApp(client.app);
|
||||
|
||||
if (!app) {
|
||||
return;
|
||||
}
|
||||
|
||||
const parsedMessage = JSON.parse(message.toString());
|
||||
|
||||
if (!parsedMessage.type) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!app.listenerCount(`ws:setTag`)) {
|
||||
app.on('ws:setTag', ({ clientId, tagKey, tagValue }) => {
|
||||
this.wsServer.setClientTag(clientId, tagKey, tagValue);
|
||||
});
|
||||
|
||||
app.on('ws:removeTag', ({ clientId, tagKey }) => {
|
||||
this.wsServer.removeClientTag(clientId, tagKey);
|
||||
});
|
||||
|
||||
app.on('ws:sendToTag', ({ tagKey, tagValue, message }) => {
|
||||
this.wsServer.sendToConnectionsByTags(
|
||||
[
|
||||
{ tagName: tagKey, tagValue },
|
||||
{ tagName: 'app', tagValue: app.name },
|
||||
],
|
||||
message,
|
||||
);
|
||||
});
|
||||
|
||||
app.on('ws:sendToTags', ({ tags, message }) => {
|
||||
this.wsServer.sendToConnectionsByTags(tags, message);
|
||||
});
|
||||
|
||||
app.on('ws:authorized', ({ clientId, userId }) => {
|
||||
this.wsServer.sendToConnectionsByTags(
|
||||
[
|
||||
{ tagName: 'userId', tagValue: userId },
|
||||
{ tagName: 'app', tagValue: app.name },
|
||||
],
|
||||
{ type: 'authorized' },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const eventName = `ws:message:${parsedMessage.type}`;
|
||||
|
||||
app.emit(eventName, {
|
||||
clientId: client.id,
|
||||
tags: [...client.tags],
|
||||
payload: parsedMessage.payload,
|
||||
});
|
||||
});
|
||||
|
||||
this.server.on('upgrade', (request, socket, head) => {
|
||||
const { pathname } = parse(request.url);
|
||||
|
||||
|
@ -16,6 +16,7 @@ import { applyErrorWithArgs, getErrorWithCode } from './errors';
|
||||
import lodash from 'lodash';
|
||||
import { Logger } from '@nocobase/logger';
|
||||
import EventEmitter from 'events';
|
||||
import { parse } from 'url';
|
||||
|
||||
declare class WebSocketWithId extends WebSocket {
|
||||
id: string;
|
||||
@ -135,6 +136,74 @@ export class WSServer extends EventEmitter {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
AppSupervisor.getInstance().on('afterAppAdded', (app) => {
|
||||
this.bindAppWSEvents(app);
|
||||
});
|
||||
|
||||
this.on('message', async ({ client, message }) => {
|
||||
const app = await AppSupervisor.getInstance().getApp(client.app);
|
||||
|
||||
if (!app) {
|
||||
return;
|
||||
}
|
||||
|
||||
const parsedMessage = JSON.parse(message.toString());
|
||||
|
||||
if (!parsedMessage.type) {
|
||||
return;
|
||||
}
|
||||
|
||||
const eventName = `ws:message:${parsedMessage.type}`;
|
||||
|
||||
app.emit(eventName, {
|
||||
clientId: client.id,
|
||||
tags: [...client.tags],
|
||||
payload: parsedMessage.payload,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
bindAppWSEvents(app) {
|
||||
if (app.listenerCount('ws:setTag') > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
app.on('ws:setTag', ({ clientId, tagKey, tagValue }) => {
|
||||
this.setClientTag(clientId, tagKey, tagValue);
|
||||
});
|
||||
|
||||
app.on('ws:removeTag', ({ clientId, tagKey }) => {
|
||||
this.removeClientTag(clientId, tagKey);
|
||||
});
|
||||
|
||||
app.on('ws:sendToTag', ({ tagKey, tagValue, message }) => {
|
||||
this.sendToConnectionsByTags(
|
||||
[
|
||||
{ tagName: tagKey, tagValue },
|
||||
{ tagName: 'app', tagValue: app.name },
|
||||
],
|
||||
message,
|
||||
);
|
||||
});
|
||||
|
||||
app.on('ws:sendToCurrentApp', ({ message }) => {
|
||||
this.sendToConnectionsByTag('app', app.name, message);
|
||||
});
|
||||
|
||||
app.on('ws:sendToTags', ({ tags, message }) => {
|
||||
this.sendToConnectionsByTags(tags, message);
|
||||
});
|
||||
|
||||
app.on('ws:authorized', ({ clientId, userId }) => {
|
||||
this.sendToConnectionsByTags(
|
||||
[
|
||||
{ tagName: 'userId', tagValue: userId },
|
||||
{ tagName: 'app', tagValue: app.name },
|
||||
],
|
||||
{ type: 'authorized' },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
addNewConnection(ws: WebSocketWithId, request: IncomingMessage) {
|
||||
@ -186,32 +255,6 @@ export class WSServer extends EventEmitter {
|
||||
if (!hasApp) {
|
||||
AppSupervisor.getInstance().bootStrapApp(handleAppName);
|
||||
}
|
||||
|
||||
// const appStatus = AppSupervisor.getInstance().getAppStatus(handleAppName, 'initializing');
|
||||
|
||||
// if (appStatus === 'not_found') {
|
||||
// this.sendMessageToConnection(client, {
|
||||
// type: 'maintaining',
|
||||
// payload: getPayloadByErrorCode('APP_NOT_FOUND', { appName: handleAppName }),
|
||||
// });
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (appStatus === 'initializing') {
|
||||
// this.sendMessageToConnection(client, {
|
||||
// type: 'maintaining',
|
||||
// payload: getPayloadByErrorCode('APP_INITIALIZING', { appName: handleAppName }),
|
||||
// });
|
||||
|
||||
// return;
|
||||
// }
|
||||
|
||||
// const app = await AppSupervisor.getInstance().getApp(handleAppName);
|
||||
//
|
||||
// this.sendMessageToConnection(client, {
|
||||
// type: 'maintaining',
|
||||
// payload: getPayloadByErrorCode(appStatus, { app }),
|
||||
// });
|
||||
}
|
||||
|
||||
removeConnection(id: string) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user