Skip to content

Commit 7677ac9

Browse files
Browser Dialer: Change from ES5 to ES6+ for performance (#3832)
1 parent bc28cad commit 7677ac9

File tree

1 file changed

+115
-106
lines changed

1 file changed

+115
-106
lines changed

transport/internet/browser_dialer/dialer.html

Lines changed: 115 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -5,132 +5,141 @@
55
</head>
66
<body>
77
<script>
8+
"use strict";
9+
// Enable a much more aggressive JIT for performance gains
10+
811
// Copyright (c) 2021 XRAY. Mozilla Public License 2.0.
9-
var url = "ws://" + window.location.host + "/websocket?token=csrfToken";
10-
var clientIdleCount = 0;
11-
var upstreamGetCount = 0;
12-
var upstreamWsCount = 0;
13-
var upstreamPostCount = 0;
14-
setInterval(check, 1000);
15-
function check() {
12+
let url = "ws://" + window.location.host + "/websocket?token=csrfToken";
13+
let clientIdleCount = 0;
14+
let upstreamGetCount = 0;
15+
let upstreamWsCount = 0;
16+
let upstreamPostCount = 0;
17+
let check = function () {
1618
if (clientIdleCount > 0) {
1719
return;
18-
}
19-
20+
};
2021
clientIdleCount += 1;
2122
console.log("Prepare", url);
22-
var ws = new WebSocket(url);
23+
let ws = new WebSocket(url);
2324
// arraybuffer is significantly faster in chrome than default
2425
// blob, tested with chrome 123
2526
ws.binaryType = "arraybuffer";
26-
ws.onmessage = function (event) {
27+
ws.addEventListener("message", (event) => {
2728
clientIdleCount -= 1;
2829
let [method, url, protocol] = event.data.split(" ");
29-
if (method == "WS") {
30-
upstreamWsCount += 1;
31-
console.log("Dial WS", url, protocol);
32-
const wss = new WebSocket(url, protocol);
33-
wss.binaryType = "arraybuffer";
34-
var opened = false;
35-
ws.onmessage = function (event) {
36-
wss.send(event.data)
37-
}
38-
wss.onopen = function (event) {
39-
opened = true;
40-
ws.send("ok")
41-
}
42-
wss.onmessage = function (event) {
43-
ws.send(event.data)
44-
}
45-
wss.onclose = function (event) {
46-
upstreamWsCount -= 1;
47-
console.log("Dial WS DONE, remaining: ", upstreamWsCount);
48-
ws.close()
49-
}
50-
wss.onerror = function (event) {
51-
!opened && ws.send("fail")
52-
wss.close()
53-
}
54-
ws.onclose = function (event) {
55-
wss.close()
56-
}
57-
} else if (method == "GET") {
58-
(async () => {
59-
console.log("Dial GET", url);
60-
ws.send("ok");
61-
const controller = new AbortController();
30+
switch (method) {
31+
case "WS": {
32+
upstreamWsCount += 1;
33+
console.log("Dial WS", url, protocol);
34+
const wss = new WebSocket(url, protocol);
35+
wss.binaryType = "arraybuffer";
36+
let opened = false;
37+
ws.onmessage = function (event) {
38+
wss.send(event.data)
39+
};
40+
wss.onopen = function (event) {
41+
opened = true;
42+
ws.send("ok")
43+
};
44+
wss.onmessage = function (event) {
45+
ws.send(event.data)
46+
};
47+
wss.onclose = function (event) {
48+
upstreamWsCount -= 1;
49+
console.log("Dial WS DONE, remaining: ", upstreamWsCount);
50+
ws.close()
51+
};
52+
wss.onerror = function (event) {
53+
!opened && ws.send("fail")
54+
wss.close()
55+
};
56+
ws.onclose = function (event) {
57+
wss.close()
58+
};
59+
break;
60+
};
61+
case "GET": {
62+
(async () => {
63+
console.log("Dial GET", url);
64+
ws.send("ok");
65+
const controller = new AbortController();
6266

63-
/*
64-
Aborting a streaming response in JavaScript
65-
requires two levers to be pulled:
67+
/*
68+
Aborting a streaming response in JavaScript
69+
requires two levers to be pulled:
6670
67-
First, the streaming read itself has to be cancelled using
68-
reader.cancel(), only then controller.abort() will actually work.
71+
First, the streaming read itself has to be cancelled using
72+
reader.cancel(), only then controller.abort() will actually work.
6973
70-
If controller.abort() alone is called while a
71-
reader.read() is ongoing, it will block until the server closes the
72-
response, the page is refreshed or the network connection is lost.
73-
*/
74+
If controller.abort() alone is called while a
75+
reader.read() is ongoing, it will block until the server closes the
76+
response, the page is refreshed or the network connection is lost.
77+
*/
7478

75-
let reader = null;
76-
ws.onclose = (event) => {
77-
try {
78-
reader && reader.cancel();
79-
} catch(e) {}
79+
let reader = null;
80+
ws.onclose = (event) => {
81+
try {
82+
reader && reader.cancel();
83+
} catch(e) {};
8084

81-
try {
82-
controller.abort();
83-
} catch(e) {}
84-
}
85+
try {
86+
controller.abort();
87+
} catch(e) {};
88+
};
8589

86-
try {
87-
upstreamGetCount += 1;
88-
const response = await fetch(url, {signal: controller.signal});
90+
try {
91+
upstreamGetCount += 1;
92+
const response = await fetch(url, {signal: controller.signal});
8993

90-
const body = await response.body;
91-
reader = body.getReader();
94+
const body = await response.body;
95+
reader = body.getReader();
9296

93-
while (true) {
94-
const { done, value } = await reader.read();
95-
ws.send(value);
96-
if (done) break;
97-
}
98-
} finally {
99-
upstreamGetCount -= 1;
100-
console.log("Dial GET DONE, remaining: ", upstreamGetCount);
101-
ws.close();
102-
}
103-
})()
104-
} else if (method == "POST") {
105-
upstreamPostCount += 1;
106-
console.log("Dial POST", url);
107-
ws.send("ok");
108-
ws.onmessage = async (event) => {
109-
try {
110-
const response = await fetch(
111-
url,
112-
{method: "POST", body: event.data}
113-
);
114-
if (response.ok) {
115-
ws.send("ok");
116-
} else {
117-
console.error("bad status code");
118-
ws.send("fail");
119-
}
120-
} finally {
121-
upstreamPostCount -= 1;
122-
console.log("Dial POST DONE, remaining: ", upstreamPostCount);
123-
ws.close();
124-
}
97+
while (true) {
98+
const { done, value } = await reader.read();
99+
ws.send(value);
100+
if (done) break;
101+
};
102+
} finally {
103+
upstreamGetCount -= 1;
104+
console.log("Dial GET DONE, remaining: ", upstreamGetCount);
105+
ws.close();
106+
};
107+
})();
108+
break;
109+
};
110+
case "POST": {
111+
upstreamPostCount += 1;
112+
console.log("Dial POST", url);
113+
ws.send("ok");
114+
ws.onmessage = async (event) => {
115+
try {
116+
const response = await fetch(
117+
url,
118+
{method: "POST", body: event.data}
119+
);
120+
if (response.ok) {
121+
ws.send("ok");
122+
} else {
123+
console.error("bad status code");
124+
ws.send("fail");
125+
};
126+
} finally {
127+
upstreamPostCount -= 1;
128+
console.log("Dial POST DONE, remaining: ", upstreamPostCount);
129+
ws.close();
130+
};
131+
};
132+
break;
125133
};
126-
}
134+
};
127135

128-
check()
129-
}
130-
ws.onerror = function (event) {
131-
ws.close()
132-
}
133-
}
136+
check();
137+
});
138+
ws.addEventListener("error", (event) => {
139+
ws.close();
140+
});
141+
};
142+
let checkTask = setInterval(check, 1000);
134143
</script>
135144
</body>
136145
</html>

0 commit comments

Comments
 (0)