From 63242a4f3ab8d7e62381d5d9d35dbd99b6235cf3 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Thu, 20 Jan 2022 02:27:49 +0800 Subject: [PATCH] refactor --- src/{client.ts => connection.ts} | 47 +++++++++++++++++++------------- src/globals.js | 9 ++++++ src/main.ts | 3 +- src/ui.js | 8 ++++++ 4 files changed, 46 insertions(+), 21 deletions(-) rename src/{client.ts => connection.ts} (69%) create mode 100644 src/globals.js diff --git a/src/client.ts b/src/connection.ts similarity index 69% rename from src/client.ts rename to src/connection.ts index 0154242cb..22746a8ed 100644 --- a/src/client.ts +++ b/src/connection.ts @@ -3,14 +3,12 @@ import * as message from "./message.js"; import * as rendezvous from "./rendezvous.js"; import { loadVp9, loadOpus } from "./codec"; -const URI = "ws://207.148.17.15"; -const PORT = 21118; +const PORT = 21116; +const HOST = "rs-sg.rustdesk.com"; const licenceKey = ""; +const SCHEMA = "ws://"; -loadVp9(); -loadOpus(); - -export default class Client { +export default class Connection { _msgs: any[]; _ws: Websock | undefined; _interval: any; @@ -18,7 +16,7 @@ export default class Client { constructor() { this._msgs = []; - this._id = ''; + this._id = ""; this._interval = setInterval(() => { while (this._msgs.length) { this._ws?.sendMessage(this._msgs[0]); @@ -32,8 +30,8 @@ export default class Client { this._ws?.close(); } - async connect(id: string) { - const ws = new Websock(URI + ":" + PORT); + async start(id: string) { + const ws = new Websock(getDefaultUri()); this._ws = ws; this._id = id; await ws.open(); @@ -65,17 +63,15 @@ export default class Client { async connectRelay(rr: rendezvous.RelayResponse) { const pk = rr.pk; let uri = rr.relayServer; - if (uri.indexOf(':') > 0) { - const tmp = uri.split(':'); - const port = parseInt(tmp[1]); - uri = tmp[0] + ':' + (port + 2); + if (uri) { + uri = getrUriFromRs(uri); } else { - uri += ':' + (PORT + 1); + uri = getDefaultUri(true); } const uuid = rr.uuid; - const ws = new Websock('ws://' + uri); + const ws = new Websock(uri); await ws.open(); - console.log('Connected to relay server') + console.log("Connected to relay server"); this._ws = ws; const requestRelay = rendezvous.RequestRelay.fromJSON({ licenceKey, @@ -91,10 +87,23 @@ export default class Client { } async function testDelay() { - const ws = new Websock(URI + ":" + PORT); + const ws = new Websock(getDefaultUri(false)); await ws.open(); console.log(ws.latency()); } -testDelay(); -new Client().connect("124931507"); +function getDefaultUri(isRelay: Boolean = false): string { + const host = localStorage.getItem("host"); + return SCHEMA + (host || HOST) + ":" + (PORT + (isRelay ? 3 : 2)); +} + +function getrUriFromRs(uri: string): string { + if (uri.indexOf(":") > 0) { + const tmp = uri.split(":"); + const port = parseInt(tmp[1]); + uri = tmp[0] + ":" + (port + 2); + } else { + uri += ":" + (PORT + 3); + } + return uri; +} diff --git a/src/globals.js b/src/globals.js new file mode 100644 index 000000000..56d4defe2 --- /dev/null +++ b/src/globals.js @@ -0,0 +1,9 @@ +window.currentConnection = undefined; + +export function setConn(conn) { + window.currentConnection = conn; +} + +export function getConn() { + return windows.currentConnection; +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 2f573e805..2be877f58 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,2 @@ -import "./style.css"; -import "./client"; +import "./globals"; import "./ui"; \ No newline at end of file diff --git a/src/ui.js b/src/ui.js index e718e7488..570265c57 100644 --- a/src/ui.js +++ b/src/ui.js @@ -1,9 +1,13 @@ +import "./style.css"; +import "./connection"; + const app = document.querySelector("#app"); if (app) { app.innerHTML = `
+
Host:
Key:
Id:
@@ -19,6 +23,8 @@ if (app) { host.value = localStorage.getItem('host'); const id = document.querySelector('#id'); id.value = localStorage.getItem('id'); + const key = document.querySelector('#key'); + key.value = localStorage.getItem('key'); }; window.connect = () => { @@ -26,6 +32,8 @@ if (app) { localStorage.setItem('host', host.value); const id = document.querySelector('#id'); localStorage.setItem('id', id.value); + const key = document.querySelector('#key'); + localStorage.setItem('key', key.value); document.querySelector('div#connect').style.display = 'none'; document.querySelector('div#password').style.display = 'block'; }