添加输出板参数调节

RPC添加超时检测
This commit is contained in:
fangxiang 2021-12-20 14:26:42 +08:00
parent 23bab5c099
commit d3d4c7c7d0
3 changed files with 84 additions and 30 deletions

View File

@ -6,6 +6,33 @@ import SubtitleEntity from "src/entities/SubtitleEntity";
import { Protocol } from "src/entities/WSProtocol"; import { Protocol } from "src/entities/WSProtocol";
import EventBus, { EventNamesDefine } from "./EventBus"; import EventBus, { EventNamesDefine } from "./EventBus";
class _RpcInfo {
send_timestamp: number;
timeout_timestamp: number;
callback: (is_fail: boolean, packet: Protocol.Commands, data: string) => void;
constructor(
timeout_timestamp: number,
callback: (
is_fail: boolean,
packet: Protocol.Commands,
data: string
) => void
) {
this.timeout_timestamp = timeout_timestamp;
this.send_timestamp = new Date().getTime();
this.callback = callback;
}
public reject() {
this.callback(true, new Protocol.Commands(), "");
}
public reslove(is_fail: boolean, packet: Protocol.Commands, data: string) {
this.callback(is_fail, packet, data);
}
}
export default class ClientConnection { export default class ClientConnection {
ws: WebSocket | null = null; ws: WebSocket | null = null;
url = ""; url = "";
@ -13,10 +40,7 @@ export default class ClientConnection {
password = ""; password = "";
_is_login = false; _is_login = false;
_rpc_id_counter = 0; _rpc_id_counter = 0;
rpc_map = new Map< rpc_map = new Map<number, _RpcInfo>();
number,
(is_fail: boolean, packet: Protocol.Commands, data: string) => void
>();
public login_callback: public login_callback:
| ((this: ClientConnection, logined: boolean) => void) | ((this: ClientConnection, logined: boolean) => void)
@ -28,6 +52,9 @@ export default class ClientConnection {
password?: string | null password?: string | null
) { ) {
this.reconnectTo(url, user_name, password); this.reconnectTo(url, user_name, password);
setTimeout(() => {
this.checkRpcTimeout();
}, 1000);
} }
get is_connected() { get is_connected() {
@ -49,6 +76,7 @@ export default class ClientConnection {
this.ws?.close(); this.ws?.close();
this.ws = null; this.ws = null;
} }
public reconnectTo( public reconnectTo(
url: string, url: string,
user_name?: string | null, user_name?: string | null,
@ -70,6 +98,14 @@ export default class ClientConnection {
this.initializeWs(); this.initializeWs();
} }
checkRpcTimeout() {
const current_datetime = new Date().getTime();
this.rpc_map.forEach((v, k, m) => {
if (current_datetime - v.send_timestamp > v.timeout_timestamp) {
}
});
}
initializeWs() { initializeWs() {
if (this.ws) { if (this.ws) {
this.ws.onclose = (ev) => { this.ws.onclose = (ev) => {
@ -180,11 +216,8 @@ export default class ClientConnection {
} }
} }
} else if (this.rpc_map.has(packet.rpc_id)) { } else if (this.rpc_map.has(packet.rpc_id)) {
const f = this.rpc_map.get(packet.rpc_id); this.rpc_map.get(packet.rpc_id)?.reslove(false, packet, ev.data);
if (f && typeof f == "function") { this.rpc_map.delete(packet.rpc_id);
f(false, packet, ev.data);
this.rpc_map.delete(packet.rpc_id);
}
} else { } else {
EventBus.getInstance().emit( EventBus.getInstance().emit(
packet.flag == Protocol.PacketEntity.FLAG_NOTIFY packet.flag == Protocol.PacketEntity.FLAG_NOTIFY
@ -214,33 +247,36 @@ export default class ClientConnection {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const rpc_id = ++this._rpc_id_counter; const rpc_id = ++this._rpc_id_counter;
if (this.rpc_map.has(rpc_id)) { if (this.rpc_map.has(rpc_id)) {
const f = this.rpc_map.get(rpc_id); this.rpc_map.get(rpc_id)?.reject();
if (f && typeof f == "function") { this.rpc_map.delete(rpc_id);
f(true, new Protocol.Commands(), "");
this.rpc_map.delete(rpc_id);
}
} }
request.rpc_id = rpc_id; request.rpc_id = rpc_id;
this.ws?.send(JSON.stringify(request));
this.rpc_map.set( this.rpc_map.set(
rpc_id, rpc_id,
(is_fail: boolean, packet: Protocol.Commands, data: string) => { new _RpcInfo(
if (is_fail) { request.timeout,
reject(); (is_fail: boolean, packet: Protocol.Commands, data: string) => {
} else { if (is_fail) {
try { reject();
const response = JSON.parse(data) as _ResponseType; } else {
if (response) { try {
resolve(response); const response = JSON.parse(data) as _ResponseType;
} else { if (response) {
resolve(response);
} else {
reject();
}
} catch {
reject(); reject();
} }
} catch {
reject();
} }
} }
} )
); );
if (request) {
request.timeout;
}
this.ws?.send(JSON.stringify(request));
}); });
} }

View File

@ -853,9 +853,25 @@ export default defineComponent({
request.rotate = output_board_rotate.value; request.rotate = output_board_rotate.value;
request.volume = output_board_volume.value; request.volume = output_board_volume.value;
request.mute = output_board_mute.value == $t.t("on"); request.mute = output_board_mute.value == $t.t("on");
GlobalData.getInstance()
.getCurrentClient() let success = false;
?.setOutputBoardSetting(request); try {
await GlobalData.getInstance()
.getCurrentClient()
?.setOutputBoardSetting(request);
success = true;
} catch {
$q.notify({
color: success ? "positive" : "negative",
icon: success ? "done" : "warning",
message:
$t.t("set output board") +
(success ? $t.t("success") : $t.t("fail")) +
"!",
position: "top",
timeout: 1000,
});
}
}; };
const apply = async () => { const apply = async () => {

View File

@ -296,6 +296,8 @@ export namespace Protocol {
rpc_id = 0; rpc_id = 0;
/** 命令 */ /** 命令 */
command = Protocol.Commands.kUnKnowCommand; command = Protocol.Commands.kUnKnowCommand;
/** 超时 毫秒 */
timeout = 15 * 1000;
} }
export class LoginRequest extends PacketEntity { export class LoginRequest extends PacketEntity {