rustdesk/src/websock.ts

98 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-01-18 02:02:39 +08:00
import * as message from "./message.js";
import * as rendezvous from "./rendezvous.js";
2022-01-19 00:57:57 +08:00
import * as sha256 from "fast-sha256";
2022-01-18 02:02:39 +08:00
type Keys = "message" | "open" | "close" | "error";
2022-01-17 18:11:14 +08:00
export default class Websock {
2022-01-18 02:02:39 +08:00
_websocket: WebSocket;
_eventHandlers: { [key in Keys]: Function };
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
send_message(msg: message.Message) {
this._websocket.send(message.Message.encode(msg).finish());
}
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
send_rendezvous(msg: rendezvous.RendezvousMessage) {
this._websocket.send(rendezvous.RendezvousMessage.encode(msg).finish());
}
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
// Event Handlers
off(evt: Keys) {
this._eventHandlers[evt] = () => {};
}
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
on(evt: Keys, handler: Function) {
this._eventHandlers[evt] = handler;
}
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
constructor(uri: string, protocols: string) {
this._eventHandlers = {
message: (_: string) => {},
open: () => {},
close: () => {},
error: () => {},
};
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
this._websocket = new WebSocket(uri, protocols);
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
this._websocket.onmessage = this._recv_message.bind(this);
this._websocket.binaryType = "arraybuffer";
this._websocket.onopen = () => {
console.debug(">> WebSock.onopen");
if (this._websocket.protocol) {
console.info("Server choose sub-protocol: " + this._websocket.protocol);
}
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
this._eventHandlers.open();
console.debug("<< WebSock.onopen");
};
this._websocket.onclose = (e) => {
console.debug(">> WebSock.onclose");
this._eventHandlers.close(e);
console.debug("<< WebSock.onclose");
};
this._websocket.onerror = (e) => {
console.debug(">> WebSock.onerror: " + e);
this._eventHandlers.error(e);
console.debug("<< WebSock.onerror: " + e);
};
}
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
close() {
if (this._websocket) {
if (
this._websocket.readyState === WebSocket.OPEN ||
this._websocket.readyState === WebSocket.CONNECTING
) {
console.info("Closing WebSocket connection");
this._websocket.close();
}
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
this._websocket.onmessage = () => {};
2022-01-17 18:11:14 +08:00
}
2022-01-18 02:02:39 +08:00
}
2022-01-17 18:11:14 +08:00
2022-01-18 02:02:39 +08:00
_recv_message(e: any) {
if (e.data instanceof window.ArrayBuffer) {
let bytes = new Uint8Array(e.data);
2022-01-17 18:11:14 +08:00
}
2022-01-18 02:02:39 +08:00
}
2022-01-19 00:57:57 +08:00
hash(datas: [Uint8Array]): Uint8Array {
const hasher = new sha256.Hash();
datas.forEach((data) => hasher.update(data));
return hasher.digest();
}
2022-01-18 02:02:39 +08:00
}
2022-01-19 19:13:49 +08:00
/*
let ws = new Websock('ws://207.148.17.15:21118');
await ws.open();
console.log("ws connected");
// let punchHole = rendezvous.PunchHoleRequest.fromJSON({ id: '' });
// ws.send_rendezvous(rendezvous.RendezvousMessage.fromJSON({ punchHole }));
let testNatRequest = rendezvous.TestNatRequest.fromJSON({ serial: 0 });
ws.send_rendezvous(rendezvous.RendezvousMessage.fromJSON({ testNatRequest }));
let msg = await ws.next();
console.log(msg);
*/