diff --git a/index.html b/index.html index 2ee0f7fbf..0ae0a2410 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,6 @@ - Vite App diff --git a/src/connection.ts b/src/connection.ts index 1272c5238..6a08d3dfb 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -7,8 +7,12 @@ import * as globals from "./globals"; import { decompress, mapKey, sleep } from "./common"; const PORT = 21116; -const HOSTS = ['rs-sg.rustdesk.com', 'rs-cn.rustdesk.com', 'rs-us.rustdesk.com']; -let HOST = localStorage.getItem('rendezvous-server') || HOSTS[0]; +const HOSTS = [ + "rs-sg.rustdesk.com", + "rs-cn.rustdesk.com", + "rs-us.rustdesk.com", +]; +let HOST = localStorage.getItem("rendezvous-server") || HOSTS[0]; const SCHEMA = "ws://"; type MsgboxCallback = (type: string, title: string, text: string) => void; @@ -38,6 +42,18 @@ export default class Connection { } async start(id: string) { + try { + await this._start(id); + } catch (e: any) { + this.msgbox( + "error", + "Connection Error", + e.type == "close" ? "Reset by the peer" : String(e) + ); + } + } + + async _start(id: string) { if (!this._options) { this._options = globals.getPeers()[id] || {}; } @@ -114,7 +130,7 @@ export default class Connection { } const uuid = rr.uuid; console.log(new Date() + ": Connecting to relay server: " + uri); - const ws = new Websock(uri, false, this.handleVideoFrame.bind(this)); + const ws = new Websock(uri, false); await ws.open(); console.log(new Date() + ": Connected to relay server"); this._ws = ws; @@ -214,7 +230,16 @@ export default class Connection { } else if (msg?.login_response) { const r = msg?.login_response; if (r.error) { - this.msgbox("error", "Error", r.error); + if (r.error == "Wrong Password") { + this._password = undefined; + this.msgbox( + "re-input-password", + r.error, + "Do you want to enter again?" + ); + } else { + this.msgbox("error", "Login Error", r.error); + } } else if (r.peer_info) { this.handlePeerInfo(r.peer_info); } @@ -239,7 +264,7 @@ export default class Connection { } else if (msg?.cursor_position) { globals.pushEvent("cursor_position", msg?.cursor_position); } else if (msg?.misc) { - this.handleMisc(msg?.misc); + if (!this.handleMisc(msg?.misc)) break; } else if (msg?.audio_frame) { globals.playAudio(msg?.audio_frame.data); } @@ -421,7 +446,10 @@ export default class Connection { handleMisc(misc: message.Misc) { if (misc.audio_format) { - globals.initAudio(misc.audio_format.channels, misc.audio_format.sample_rate); + globals.initAudio( + misc.audio_format.channels, + misc.audio_format.sample_rate + ); } else if (misc.chat_message) { globals.pushEvent("chat", misc.chat_message.text); } else if (misc.permission_info) { @@ -447,7 +475,10 @@ export default class Connection { globals.pushEvent("switch_display", misc.switch_display); } else if (misc.close_reason) { this.msgbox("error", "Connection Error", misc.close_reason); + this.close(); + return false; } + return true; } getRemember(): Boolean { @@ -485,16 +516,16 @@ export default class Connection { ) { const key_event = mapKey(name, globals.isDesktop()); if (!key_event) return; - if (alt && (name == "Alt" || name == 'RAlt')) { + if (alt && (name == "Alt" || name == "RAlt")) { alt = false; } - if (ctrl && (name == "Control" || name == 'RControl')) { + if (ctrl && (name == "Control" || name == "RControl")) { ctrl = false; } - if (shift && (name == "Shift" || name == 'RShift')) { + if (shift && (name == "Shift" || name == "RShift")) { shift = false; } - if (command && (name == "Meta" || name == 'RWin')) { + if (command && (name == "Meta" || name == "RWin")) { command = false; } key_event.down = down; @@ -648,14 +679,14 @@ export default class Connection { } function testDelay() { - var nearest = ''; + var nearest = ""; HOSTS.forEach((host) => { const now = new Date().getTime(); new Websock(getrUriFromRs(host), true).open().then(() => { - console.log('latency of ' + host + ': ' + (new Date().getTime() - now)); + console.log("latency of " + host + ": " + (new Date().getTime() - now)); if (!nearest) { HOST = host; - localStorage.setItem('rendezvous-server', host); + localStorage.setItem("rendezvous-server", host); } }); }); diff --git a/src/globals.js b/src/globals.js index ecca1ee3a..dd96404f0 100644 --- a/src/globals.js +++ b/src/globals.js @@ -28,7 +28,7 @@ export function msgbox(type, title, text) { if (!events) return; if (!type || (type == 'error' && !text)) return; const text2 = text.toLowerCase(); - var hasRetry = checkIfRetry(type, title, text); + var hasRetry = checkIfRetry(type, title, text) ? 'true' : ''; events.push({ name: 'msgbox', type, title, text, hasRetry }); } @@ -63,15 +63,10 @@ export function getConn() { } export async function startConn(id) { - try { - currentFrame = undefined; - events = []; - setByName('remote_id', id); - await curConn.start(id); - } catch (e) { - console.log(e); - msgbox('error', 'Error', String(e)); - } + currentFrame = undefined; + events = []; + setByName('remote_id', id); + await curConn.start(id); } export function close() { @@ -308,6 +303,7 @@ window.init = async () => { } loadVp9(() => { }); await initZstd(); + console.log('init done'); } export function getPeers() { diff --git a/src/websock.ts b/src/websock.ts index d5652895d..6be6b1d16 100644 --- a/src/websock.ts +++ b/src/websock.ts @@ -13,13 +13,8 @@ export default class Websock { _secretKey: [Uint8Array, number, number] | undefined; _uri: string; _isRendezvous: boolean; - _videoCallback: ((v: message.VideoFrame) => void) | undefined; - constructor( - uri: string, - isRendezvous: boolean = true, - fn: ((v: message.VideoFrame) => void) | undefined = undefined - ) { + constructor(uri: string, isRendezvous: boolean = true) { this._eventHandlers = { message: (_: any) => {}, open: () => {}, @@ -34,7 +29,6 @@ export default class Websock { this._websocket.binaryType = "arraybuffer"; this._latency = new Date().getTime(); this._isRendezvous = isRendezvous; - this._videoCallback = fn; } latency(): number { @@ -106,11 +100,13 @@ export default class Websock { this._websocket.onclose = (e) => { if (this._status == "open") { // e.code 1000 means that the connection was closed normally. - reject("Reset by the peer"); + // } this._status = e; - console.error("WebSock.onclose: " + e); + console.error("WebSock.onclose: "); + console.error(e); this._eventHandlers.close(e); + reject("Reset by the peer"); }; this._websocket.onerror = (e: any) => { if (!this._status) { @@ -118,9 +114,9 @@ export default class Websock { return; } this._status = e; - console.error("WebSock.onerror: " + e); + console.error("WebSock.onerror: ") + console.error(e); this._eventHandlers.error(e); - reject(e["data"]); }; }); } @@ -176,16 +172,11 @@ export default class Websock { k[2] += 1; bytes = globals.decrypt(bytes, k[2], k[0]); } - if (this._isRendezvous) { - this._buf.push(this.parseRendezvous(bytes)); - } else { - const m = this.parseMessage(bytes); - if (this._videoCallback && m.video_frame) { - this._videoCallback(m.video_frame); - } else { - this._buf.push(m); - } - } + this._buf.push( + this._isRendezvous + ? this.parseRendezvous(bytes) + : this.parseMessage(bytes) + ); } this._eventHandlers.message(e.data); }