will do more on socket error

This commit is contained in:
rustdesk 2022-01-27 01:30:29 +08:00
parent fad130b29a
commit 6c0030bbe2
4 changed files with 43 additions and 22 deletions

View File

@ -6,8 +6,8 @@ import * as sha256 from "fast-sha256";
import * as globals from "./globals";
const PORT = 21116;
const HOST = 'rs-sg.rustdesk.com';
const SCHEMA = 'ws://';
const HOST = "rs-sg.rustdesk.com";
const SCHEMA = "ws://";
type MsgboxCallback = (type: string, title: string, text: string) => void;
type DrawCallback = (data: Uint8Array) => void;
@ -36,7 +36,8 @@ export default class Connection {
async start(id: string) {
try {
this._options = JSON.parse((localStorage.getItem('peers') || '{}'))[id] || {};
this._options =
JSON.parse(localStorage.getItem("peers") || "{}")[id] || {};
} catch (e) {
this._options = {};
}
@ -66,7 +67,7 @@ export default class Connection {
const natType = rendezvous.NatType.SYMMETRIC;
const punchHoleRequest = rendezvous.PunchHoleRequest.fromPartial({
id,
licenceKey: localStorage.getItem('key') || undefined,
licenceKey: localStorage.getItem("key") || undefined,
connType,
natType,
});
@ -114,7 +115,7 @@ export default class Connection {
console.log(new Date() + ": Connected to relay server");
this._ws = ws;
const requestRelay = rendezvous.RequestRelay.fromPartial({
licenceKey: localStorage.getItem('key') || undefined,
licenceKey: localStorage.getItem("key") || undefined,
uuid,
});
ws.sendRendezvous({ requestRelay });
@ -351,7 +352,7 @@ export default class Connection {
}
getRemember(): any {
return this._options['remember'];
return this._options["remember"];
}
getOption(name: string): any {

View File

@ -13,16 +13,16 @@ window.getRgba = () => currentFrame;
window.getLanguage = () => navigator.language;
export function msgbox(type, title, text) {
text = text.toLowerCase();
var hasRetry = msgtype == "error"
const text2 = text.toLowerCase();
var hasRetry = type == "error"
&& title == "Connection Error"
&& !text.indexOf("offline") >= 0
&& !text.indexOf("exist") >= 0
&& !text.indexOf("handshake") >= 0
&& !text.indexOf("failed") >= 0
&& !text.indexOf("resolve") >= 0
&& !text.indexOf("mismatch") >= 0
&& !text.indexOf("manually") >= 0;
&& text2.indexOf("offline") < 0
&& text2.indexOf("exist") < 0
&& text2.indexOf("handshake") < 0
&& text2.indexOf("failed") < 0
&& text2.indexOf("resolve") < 0
&& text2.indexOf("mismatch") < 0
&& text2.indexOf("manually") < 0;
events.push({ name: 'msgbox', type, title, text, hasRetry });
}
@ -43,6 +43,15 @@ export function getConn() {
return window.curConn;
}
export async function startConn(id) {
try {
await curConn.start(id);
} catch (e) {
console.log(e);
msgbox('error', 'Error', String(e));
}
}
export function close() {
getConn()?.close();
setConn(undefined);
@ -128,11 +137,11 @@ export function decompress(compressedArray) {
window.setByName = (name, value) => {
try {
value = JSON.parse(value);
} catch (e) {}
} catch (e) { }
switch (name) {
case 'connect':
newConn();
curConn.start(value);
startConn(value);
break;
case 'login':
curConn.login(value.password, value.remember || false);
@ -214,10 +223,10 @@ window.setByName = (name, value) => {
window.getByName = (name, arg) => {
try {
arg = JSON.parse(arg);
} catch (e) {}
} catch (e) { }
switch (name) {
case 'peers':
return localStorage.getItem('peers');
return localStorage.getItem('peers') || '[]';
break;
case 'remote_id':
return localStorage.getItem('remote-id') || '';
@ -247,7 +256,10 @@ window.getByName = (name, arg) => {
case 'peer_option':
return curConn.getOption(arg);
break;
case 'test_if_valid_server':
break;
}
return '';
}
window.init = () => {

View File

@ -31,7 +31,7 @@ if (app) {
document.body.onload = () => {
const host = document.querySelector('#host');
host.value = localStorage.getItem('host');
host.value = localStorage.getItem('custom-rendezvous-server');
const id = document.querySelector('#id');
id.value = localStorage.getItem('id');
const key = document.querySelector('#key');
@ -41,7 +41,7 @@ if (app) {
window.connect = () => {
const host = document.querySelector('#host');
localStorage.setItem('host', host.value);
localStorage.setItem('custom-rendezvous-server', host.value);
const id = document.querySelector('#id');
localStorage.setItem('id', id.value);
const key = document.querySelector('#key');

View File

@ -11,6 +11,7 @@ export default class Websock {
_status: any;
_latency: number;
_secretKey: [Uint8Array, number, number] | undefined;
_uri: string;
constructor(uri: string) {
this._eventHandlers = {
@ -19,6 +20,7 @@ export default class Websock {
close: () => {},
error: () => {},
};
this._uri = uri;
this._status = "";
this._buf = [];
this._websocket = new WebSocket(uri);
@ -36,7 +38,9 @@ export default class Websock {
}
sendMessage(json: any) {
let data = message.Message.encode(message.Message.fromPartial(json)).finish();
let data = message.Message.encode(
message.Message.fromPartial(json)
).finish();
let k = this._secretKey;
if (k) {
k[1] += 1;
@ -98,6 +102,10 @@ export default class Websock {
reject(e);
};
this._websocket.onerror = (e) => {
if (!this._status) {
reject('Failed to connect to ' + this._uri);
return;
}
this._status = e;
console.error("WebSock.onerror: " + e);
this._eventHandlers.error(e);