forked from zizifn/edgetunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws-send-issue.js
60 lines (56 loc) · 2.02 KB
/
ws-send-issue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const chunk = '0'.repeat(1024 * 5);
export default {
async fetch(request, env, ctx) {
try {
console.log('---------------');
const webSocketPair = new WebSocketPair();
/** @type {import("@cloudflare/workers-types").WebSocket[]} */
const [client, webSocket] = Object.values(webSocketPair);
webSocket.accept();
let btyes = 0;
// (async () => {
// const repose = await fetch('http://speed.cloudflare.com/__down?bytes=1145141919810')
// const body = repose.body;
// const reader = body?.getReader();
// let packets = [];
// while (true && reader) {
// const { done, value } = await reader.read();
// packets.push(value);
// console.log(btyes += value?.length || 0);
// if (packets.length > 100) {
// webSocket.send(value || '');
// await delay(2);
// packets = [];
// }
// if (done) {
// break;
// }
// }
// })()
console.log('---------------');
(async () => {
let packets = [];
console.log('---------------');
while (true) {
console.log(btyes += chunk?.length || 0);
webSocket.send(chunk || '');
await delay(1)
}
})()
// console.log(btyes += chunk?.length || 0);
// webSocket.send(chunk || '');
return new Response(null, {
status: 101,
webSocket: client,
});
} catch (err) {
/** @type {Error} */ let e = err;
return new Response(e.toString());
}
},
};
function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}