websock init

This commit is contained in:
rustdesk 2022-01-18 02:02:39 +08:00
parent fa31257cdc
commit d4e8a66cf6

View File

@ -1,45 +1,43 @@
import * as proto from '../message.js'; import * as message from "./message.js";
import * as rendezvous from "./rendezvous.js";
type Keys = "message" | "open" | "close" | "error";
export default class Websock { export default class Websock {
constructor() { _websocket: WebSocket;
this._websocket = null; // WebSocket object _eventHandlers: { [key in Keys]: Function };
this._eventHandlers = {
message: (msg) => { }, send_message(msg: message.Message) {
open: () => { }, this._websocket.send(message.Message.encode(msg).finish());
close: () => { },
error: () => { }
};
this._next_yuv = null;
this._next_rgb = null;
} }
send(msg) { send_rendezvous(msg: rendezvous.RendezvousMessage) {
if (msg instanceof Object) msg = proto.encodeMessage(msg); this._websocket.send(rendezvous.RendezvousMessage.encode(msg).finish());
this._websocket.send(msg);
} }
// Event Handlers // Event Handlers
off(evt) { off(evt: Keys) {
this._eventHandlers[evt] = () => { }; this._eventHandlers[evt] = () => {};
} }
on(evt, handler) { on(evt: Keys, handler: Function) {
this._eventHandlers[evt] = handler; this._eventHandlers[evt] = handler;
} }
init() { constructor(uri: string, protocols: string) {
this._websocket = null; this._eventHandlers = {
} message: (_: string) => {},
open: () => {},
open(uri, protocols) { close: () => {},
this.init(); error: () => {},
};
this._websocket = new WebSocket(uri, protocols); this._websocket = new WebSocket(uri, protocols);
this._websocket.onmessage = this._recv_message.bind(this); this._websocket.onmessage = this._recv_message.bind(this);
this._websocket.binaryType = 'arraybuffer'; this._websocket.binaryType = "arraybuffer";
this._websocket.onopen = () => { this._websocket.onopen = () => {
console.debug('>> WebSock.onopen'); console.debug(">> WebSock.onopen");
if (this._websocket.protocol) { if (this._websocket.protocol) {
console.info("Server choose sub-protocol: " + this._websocket.protocol); console.info("Server choose sub-protocol: " + this._websocket.protocol);
} }
@ -61,56 +59,21 @@ export default class Websock {
close() { close() {
if (this._websocket) { if (this._websocket) {
if ((this._websocket.readyState === WebSocket.OPEN) || if (
(this._websocket.readyState === WebSocket.CONNECTING)) { this._websocket.readyState === WebSocket.OPEN ||
this._websocket.readyState === WebSocket.CONNECTING
) {
console.info("Closing WebSocket connection"); console.info("Closing WebSocket connection");
this._websocket.close(); this._websocket.close();
} }
this._websocket.onmessage = () => { }; this._websocket.onmessage = () => {};
} }
} }
_recv_message(e) { _recv_message(e: any) {
if (e.data instanceof window.ArrayBuffer) { if (e.data instanceof window.ArrayBuffer) {
let bytes = new Uint8Array(e.data); let bytes = new Uint8Array(e.data);
if (this._next_yuv) {
const yuv = this._next_yuv;
const { compress, stride } = yuv.format;
if (compress) {
bytes = window.simple_zstd.decompress(bytes);
}
if (!yuv.y) {
yuv.y = { bytes, stride: stride };
} else if (!yuv.u) {
yuv.u = { bytes, stride: stride >> 1 };
} else {
yuv.v = { bytes, stride: stride >> 1 };
delete yuv.format.stride;
this._eventHandlers.message({ video_frame: { yuv } });
this._next_yuv = null;
}
} else if (this._next_rgb) {
if (this._next_rgb.format.compress) {
bytes = window.simple_zstd.decompress(bytes);
}
this._eventHandlers.message({ video_frame: { rgb: bytes } });
this._next_rgb = null;
} else {
const msg = proto.decodeMessage(bytes);
let vf = msg.video_frame;
if (vf) {
const { yuv, rgb } = vf;
if (yuv) {
this._next_yuv = { format: yuv };
} else if (rgb) {
this._next_rgb = { format: rgb };
}
return;
} else {
this._eventHandlers.message(msg);
}
}
} }
} }
} }