socket.writable.getWriter().write() hangs at 7th write

hello, i have a Cloudflare Workers to act like a "server ping service" using cloudflare:sockets. Below is my code for pinging an IP address using sockets, with amount time, and server is the WebSockets Server connected to client for sending back the ping data:
const pingServer = async () => {
for (let i = 0; i < amount; i++) {
try {
server.send(JSON.stringify({ message: "connecting" }));
const start = Date.now();
const socket = await connect({
hostname: ip,
port,
});
server.send(JSON.stringify({ message: "connected, getting writer" }));
const writer = socket.writable.getWriter()
const randomData = new Uint8Array(32);
crypto.getRandomValues(randomData);

server.send(JSON.stringify({ message: `operation ${i+1}, writing data ${randomData}` }));
await writer.write(randomData);
server.send(JSON.stringify({ message: "data written, waiting for read" }));

// Read with a timeout
const delay = await Promise.race([
(async () => {
await socket.readable.getReader().read();
return Date.now() - start;
})(),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("Timed out")), timeout)
),
]);

server.send(JSON.stringify({ success: true, delay: delay }));
server.send(JSON.stringify({ message: "closing connection" }));
socket.close();
server.send(JSON.stringify({ message: "connection closed" }));

} catch (err) {
server.send(
JSON.stringify({ success: false, delay: -1, error: err.message })
);
}

await new Promise((resolve) => setTimeout(resolve, timeout));
}

server.close();
};
const pingServer = async () => {
for (let i = 0; i < amount; i++) {
try {
server.send(JSON.stringify({ message: "connecting" }));
const start = Date.now();
const socket = await connect({
hostname: ip,
port,
});
server.send(JSON.stringify({ message: "connected, getting writer" }));
const writer = socket.writable.getWriter()
const randomData = new Uint8Array(32);
crypto.getRandomValues(randomData);

server.send(JSON.stringify({ message: `operation ${i+1}, writing data ${randomData}` }));
await writer.write(randomData);
server.send(JSON.stringify({ message: "data written, waiting for read" }));

// Read with a timeout
const delay = await Promise.race([
(async () => {
await socket.readable.getReader().read();
return Date.now() - start;
})(),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("Timed out")), timeout)
),
]);

server.send(JSON.stringify({ success: true, delay: delay }));
server.send(JSON.stringify({ message: "closing connection" }));
socket.close();
server.send(JSON.stringify({ message: "connection closed" }));

} catch (err) {
server.send(
JSON.stringify({ success: false, delay: -1, error: err.message })
);
}

await new Promise((resolve) => setTimeout(resolve, timeout));
}

server.close();
};
however, at the await writer.write(randomData);, this always hang at 7th operation and I don't know why. can someone help me with this?
No description
1 Reply
Ankita Tudubucket
Ankita TudubucketOP•2mo ago
I will connect to my workers through ws, then send ping request data to workers, then it will start sending back the ping data. and as the title described, it will hangs on 7th ping but there is a very awkard thing that, if i continue send another ping request to my workers, even when it is hanging, it will continue the 1st job (not hanging anymore) and start 2nd job, until the 1st job exceed the ping amount and close the ws still got no solution, can someone help me out of this 😅😅
Want results from more Discord servers?
Add your server