Commit ced68d12 authored by nanahira's avatar nanahira

configurable bufferMessage

parent 887da7ef
......@@ -38,6 +38,7 @@ routes:
heartbeat: 3000 # 心跳包的间隔。0 或不填为禁用心跳包。
readonly: false # 该路由是否为只读。只读路由的连接无法对机器人进行写操作,只会得到模拟响应,但是可以进行 get 操作以及接收事件。
rateLimitInterval: 500 # 限速调用间隔,默认 500ms。
bufferMessage: false # 是否在 app 断线期间缓存消息,并在 app 恢复连接时发送。
wsReverse: # 该路由的反向 WebSocket 配置。可以配置多个。
- endpoint: 'ws://localhost:8080'
token: 'token'
......
......@@ -25,6 +25,7 @@ routes:
heartbeat: 3000 # 心跳包的间隔。0 或不填为禁用心跳包。
readonly: false # 该路由是否为只读。只读路由的连接无法对机器人进行写操作,只会得到模拟响应,但是可以进行 get 操作以及接收事件。
rateLimitInterval: 500 # 限速调用间隔,默认 500ms。
bufferMessage: false # 是否在 app 断线期间缓存消息,并在 app 恢复连接时发送。
wsReverse: # 该路由的反向 WebSocket 配置。可以配置多个。
- endpoint: 'ws://localhost:8080'
token: 'token'
......
......@@ -24,6 +24,7 @@ export interface RouteConfig {
readonly?: boolean;
rateLimitInterval?: number;
reverseWs?: ReverseWsConfig[];
bufferMessage?: boolean;
}
export class Route implements RouteConfig {
private connections: WebSocket[] = [];
......@@ -39,6 +40,7 @@ export class Route implements RouteConfig {
reverseWs?: ReverseWsConfig[];
readonly?: boolean;
rateLimitInterval: number;
bufferMessage?: boolean;
preMessages: { data: any; session: Session }[] = [];
constructor(routeConfig: RouteConfig, ctx: Context) {
Object.assign(this, routeConfig);
......@@ -66,7 +68,9 @@ export class Route implements RouteConfig {
}
send(data: any, session: Session, allConns = this.connections) {
if (!allConns.length) {
this.preMessages.push({ data, session });
if (this.bufferMessage) {
this.preMessages.push({ data, session });
}
return;
}
const message = JSON.stringify(data);
......@@ -156,6 +160,9 @@ export class Route implements RouteConfig {
}
addConnection(conn: WebSocket) {
this.connections.push(conn);
if (!this.bufferMessage) {
return;
}
const preMessages = this.preMessages;
this.preMessages = [];
for (const message of preMessages) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment