添加 @kevisual/api 依赖,更新 WebSocket 消息发送逻辑,支持上下文参数
This commit is contained in:
@@ -2,6 +2,7 @@ import { nanoid } from 'nanoid';
|
||||
import { WebSocket } from 'ws';
|
||||
import { logger } from '../logger.ts';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
|
||||
class WsMessage {
|
||||
ws: WebSocket;
|
||||
user?: string;
|
||||
@@ -16,7 +17,7 @@ class WsMessage {
|
||||
this.emitter.emit(data.id, data?.data);
|
||||
}
|
||||
}
|
||||
async sendData(data: any, opts?: { timeout?: number }) {
|
||||
async sendData(data: any, context?: any, opts?: { timeout?: number }) {
|
||||
if (this.ws.readyState !== WebSocket.OPEN) {
|
||||
return { code: 500, message: 'WebSocket is not open' };
|
||||
}
|
||||
@@ -25,7 +26,10 @@ class WsMessage {
|
||||
const message = JSON.stringify({
|
||||
id,
|
||||
type: 'proxy',
|
||||
data,
|
||||
data: {
|
||||
message: data,
|
||||
context: context || {},
|
||||
},
|
||||
});
|
||||
logger.info('ws-proxy sendData', message);
|
||||
this.ws.send(message);
|
||||
|
||||
@@ -74,7 +74,10 @@ export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opt
|
||||
res.end(await html);
|
||||
return true;
|
||||
}
|
||||
const value = await client.sendData(data);
|
||||
// const { token, cookies, ...rest } = data;
|
||||
const value = await client.sendData(data, {
|
||||
state: { tokenUser: loginUser.tokenUser },
|
||||
});
|
||||
if (value) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify(value));
|
||||
|
||||
@@ -282,6 +282,8 @@ app
|
||||
ctx.throw('app 未发现');
|
||||
}
|
||||
if (detect) {
|
||||
const appKey = appList.key;
|
||||
const version = appList.version;
|
||||
// 自动检测最新版本
|
||||
const res = await callDetectAppVersion({ appKey, version, username: username || tokenUser.username }, ctx.query.token);
|
||||
if (res.code !== 200) {
|
||||
|
||||
Reference in New Issue
Block a user