-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NetworkError caused by WebSocketStream cannot be caught? #22165
Comments
It looks like you also need to await const listener = Deno.listen({ port: 9000, transport: "tcp" });
try {
for await (const conn of listener) {
(async () => {
const ws = new WebSocketStream("ws://loca99999999999lhost:8000", {
headers: {
"x-Protocol": "Transmission Control",
"X-Destination-Address": "www.ba99999999999idu.com",
"X-Destination-Port": "80",
},
});
// console.log(ws);
try {
const webConn = await ws.opened; //.catch(console.error);
// console.log(webConn);
conn.readable.pipeTo(webConn.writable).catch(function (e) {
try {
ws.close();
conn.close();
} catch (e) {
console.error(e);
}
return console.error(e);
});
webConn.readable.pipeTo(conn.writable).catch(function (e) {
try {
ws.close();
conn.close();
} catch (e) {
console.error(e);
}
return console.error(e);
});
} catch (error) {
console.error(error);
try {
ws.close();
conn.close();
+ await ws.closed;
} catch (e) {
console.error(e);
}
}
})().catch(console.error);
}
} catch (error) {
console.error(error);
} |
While this can be solved, can we design a better WebSocketStream api? |
export class WebSocketStream {
public socket: WebSocket;
public readable: ReadableStream<Uint8Array>;
public writable: WritableStream<Uint8Array>;
constructor(socket: WebSocket) {
this.socket = socket;
this.readable = new ReadableStream({
start(controller) {
socket.onmessage = function ({ data }) {
// console.log(data);
return controller.enqueue(new Uint8Array(data));
};
socket.onerror = (e) => controller.error(e);
socket.onclose = (/* e */) => controller.close(/* e */);
},
cancel(/* reason */) {
socket.close();
},
});
this.writable = new WritableStream({
start(controller) {
socket.onerror = (e) => controller.error(e);
},
write(chunk) {
// console.log(chunk);
socket.send(chunk);
},
close() {
socket.close();
},
abort(e) {
console.error(e);
socket.close();
},
});
}
} |
ref: #8831 |
Closing as the original issue is solved and nothing is actionable further.
You are encouraged to create 3rd party module to address those pain points |
Version: Deno 1.40.1
The text was updated successfully, but these errors were encountered: