parent
23bab5c099
commit
d3d4c7c7d0
|
@ -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") {
|
|
||||||
f(false, packet, ev.data);
|
|
||||||
this.rpc_map.delete(packet.rpc_id);
|
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,16 +247,14 @@ 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") {
|
|
||||||
f(true, new Protocol.Commands(), "");
|
|
||||||
this.rpc_map.delete(rpc_id);
|
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,
|
||||||
|
new _RpcInfo(
|
||||||
|
request.timeout,
|
||||||
(is_fail: boolean, packet: Protocol.Commands, data: string) => {
|
(is_fail: boolean, packet: Protocol.Commands, data: string) => {
|
||||||
if (is_fail) {
|
if (is_fail) {
|
||||||
reject();
|
reject();
|
||||||
|
@ -240,7 +271,12 @@ export default class ClientConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
if (request) {
|
||||||
|
request.timeout;
|
||||||
|
}
|
||||||
|
this.ws?.send(JSON.stringify(request));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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()
|
|
||||||
|
let success = false;
|
||||||
|
try {
|
||||||
|
await GlobalData.getInstance()
|
||||||
.getCurrentClient()
|
.getCurrentClient()
|
||||||
?.setOutputBoardSetting(request);
|
?.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 () => {
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue