mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-22 05:06:07 +03:00
Browser Dialer: Change from ES5 to ES6+ for performance (#3832)
This commit is contained in:
parent
bc28cad8f8
commit
7677ac980d
@ -5,56 +5,60 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script>
|
<script>
|
||||||
|
"use strict";
|
||||||
|
// Enable a much more aggressive JIT for performance gains
|
||||||
|
|
||||||
// Copyright (c) 2021 XRAY. Mozilla Public License 2.0.
|
// Copyright (c) 2021 XRAY. Mozilla Public License 2.0.
|
||||||
var url = "ws://" + window.location.host + "/websocket?token=csrfToken";
|
let url = "ws://" + window.location.host + "/websocket?token=csrfToken";
|
||||||
var clientIdleCount = 0;
|
let clientIdleCount = 0;
|
||||||
var upstreamGetCount = 0;
|
let upstreamGetCount = 0;
|
||||||
var upstreamWsCount = 0;
|
let upstreamWsCount = 0;
|
||||||
var upstreamPostCount = 0;
|
let upstreamPostCount = 0;
|
||||||
setInterval(check, 1000);
|
let check = function () {
|
||||||
function check() {
|
|
||||||
if (clientIdleCount > 0) {
|
if (clientIdleCount > 0) {
|
||||||
return;
|
return;
|
||||||
}
|
};
|
||||||
|
|
||||||
clientIdleCount += 1;
|
clientIdleCount += 1;
|
||||||
console.log("Prepare", url);
|
console.log("Prepare", url);
|
||||||
var ws = new WebSocket(url);
|
let ws = new WebSocket(url);
|
||||||
// arraybuffer is significantly faster in chrome than default
|
// arraybuffer is significantly faster in chrome than default
|
||||||
// blob, tested with chrome 123
|
// blob, tested with chrome 123
|
||||||
ws.binaryType = "arraybuffer";
|
ws.binaryType = "arraybuffer";
|
||||||
ws.onmessage = function (event) {
|
ws.addEventListener("message", (event) => {
|
||||||
clientIdleCount -= 1;
|
clientIdleCount -= 1;
|
||||||
let [method, url, protocol] = event.data.split(" ");
|
let [method, url, protocol] = event.data.split(" ");
|
||||||
if (method == "WS") {
|
switch (method) {
|
||||||
|
case "WS": {
|
||||||
upstreamWsCount += 1;
|
upstreamWsCount += 1;
|
||||||
console.log("Dial WS", url, protocol);
|
console.log("Dial WS", url, protocol);
|
||||||
const wss = new WebSocket(url, protocol);
|
const wss = new WebSocket(url, protocol);
|
||||||
wss.binaryType = "arraybuffer";
|
wss.binaryType = "arraybuffer";
|
||||||
var opened = false;
|
let opened = false;
|
||||||
ws.onmessage = function (event) {
|
ws.onmessage = function (event) {
|
||||||
wss.send(event.data)
|
wss.send(event.data)
|
||||||
}
|
};
|
||||||
wss.onopen = function (event) {
|
wss.onopen = function (event) {
|
||||||
opened = true;
|
opened = true;
|
||||||
ws.send("ok")
|
ws.send("ok")
|
||||||
}
|
};
|
||||||
wss.onmessage = function (event) {
|
wss.onmessage = function (event) {
|
||||||
ws.send(event.data)
|
ws.send(event.data)
|
||||||
}
|
};
|
||||||
wss.onclose = function (event) {
|
wss.onclose = function (event) {
|
||||||
upstreamWsCount -= 1;
|
upstreamWsCount -= 1;
|
||||||
console.log("Dial WS DONE, remaining: ", upstreamWsCount);
|
console.log("Dial WS DONE, remaining: ", upstreamWsCount);
|
||||||
ws.close()
|
ws.close()
|
||||||
}
|
};
|
||||||
wss.onerror = function (event) {
|
wss.onerror = function (event) {
|
||||||
!opened && ws.send("fail")
|
!opened && ws.send("fail")
|
||||||
wss.close()
|
wss.close()
|
||||||
}
|
};
|
||||||
ws.onclose = function (event) {
|
ws.onclose = function (event) {
|
||||||
wss.close()
|
wss.close()
|
||||||
}
|
};
|
||||||
} else if (method == "GET") {
|
break;
|
||||||
|
};
|
||||||
|
case "GET": {
|
||||||
(async () => {
|
(async () => {
|
||||||
console.log("Dial GET", url);
|
console.log("Dial GET", url);
|
||||||
ws.send("ok");
|
ws.send("ok");
|
||||||
@ -76,12 +80,12 @@
|
|||||||
ws.onclose = (event) => {
|
ws.onclose = (event) => {
|
||||||
try {
|
try {
|
||||||
reader && reader.cancel();
|
reader && reader.cancel();
|
||||||
} catch(e) {}
|
} catch(e) {};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
} catch(e) {}
|
} catch(e) {};
|
||||||
}
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
upstreamGetCount += 1;
|
upstreamGetCount += 1;
|
||||||
@ -94,14 +98,16 @@
|
|||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
ws.send(value);
|
ws.send(value);
|
||||||
if (done) break;
|
if (done) break;
|
||||||
}
|
};
|
||||||
} finally {
|
} finally {
|
||||||
upstreamGetCount -= 1;
|
upstreamGetCount -= 1;
|
||||||
console.log("Dial GET DONE, remaining: ", upstreamGetCount);
|
console.log("Dial GET DONE, remaining: ", upstreamGetCount);
|
||||||
ws.close();
|
ws.close();
|
||||||
}
|
};
|
||||||
})()
|
})();
|
||||||
} else if (method == "POST") {
|
break;
|
||||||
|
};
|
||||||
|
case "POST": {
|
||||||
upstreamPostCount += 1;
|
upstreamPostCount += 1;
|
||||||
console.log("Dial POST", url);
|
console.log("Dial POST", url);
|
||||||
ws.send("ok");
|
ws.send("ok");
|
||||||
@ -116,21 +122,24 @@
|
|||||||
} else {
|
} else {
|
||||||
console.error("bad status code");
|
console.error("bad status code");
|
||||||
ws.send("fail");
|
ws.send("fail");
|
||||||
}
|
};
|
||||||
} finally {
|
} finally {
|
||||||
upstreamPostCount -= 1;
|
upstreamPostCount -= 1;
|
||||||
console.log("Dial POST DONE, remaining: ", upstreamPostCount);
|
console.log("Dial POST DONE, remaining: ", upstreamPostCount);
|
||||||
ws.close();
|
ws.close();
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
check()
|
check();
|
||||||
}
|
});
|
||||||
ws.onerror = function (event) {
|
ws.addEventListener("error", (event) => {
|
||||||
ws.close()
|
ws.close();
|
||||||
}
|
});
|
||||||
}
|
};
|
||||||
|
let checkTask = setInterval(check, 1000);
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user