cloudflare / cloudflare/workers-rs
[BUG] WebSocket::connect(...) not working
- Dominant language
- Rust
- Stars
- 3.7k
- Forks
- 429
- Avg merge
- 20h 28m
- Merged PRs (30d)
- 7
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### What version of `workers-rs` are you using?
current
### Describe the bug
Pay attention it reproduced on main branch
While integration I tried to connect to WS host using this construction WebSocket::connect, but getting error even in examples
let ws = WebSocket::connect("ws://{url}/websocket".parse()?).await?;
While debugging got that
let res = Fetch::Request(req).send().await?;
returns this response:
{ body: Empty, headers: Headers {
sec-websocket-accept = J+4ZKPYPix37ekSybZ9fd4fQfMY=
upgrade = websocket
}
, status_code: 101, websocket: None }
You can see that websocket is None
with worker version 0.0.13 everything works fine. This regression reproduced on main branch and also on branches, rebased on to main
### Steps To Reproduce
1. Just add router from example
2. router
.get_async("/ws-client", |_, _| async move {
let ws = WebSocket::connect("ws://127.0.0.1:8787/websocket".parse()?).await?;
let mut event_stream = ws.events()?;
ws.accept()?;
ws.send_with_str("Hello, world!")?;
while let Some(event) = event_stream.next().await {
let event = event?;
if let WebsocketEvent::Message(msg) = event {
if let Some(text) = msg.text() {
return Response::ok(text);
}
}
}
Response::error("never got a message echoed back :(", 500)
})
.get_async("/websocket", |_, _ctx| async move {
let pair = WebSocketPair::new()?;
let server = pair.server;
server.accept()?;
wasm_bindgen_futures::spawn_local(async move {
let mut event_stream = server.events().expect("could not open stream");
while let Some(event) = event_stream.next().await {
match event.expect("received error in websocket") {
WebsocketEvent::Message(msg) => {
if let Some(text) = msg.text() {
server.send_with_str(text).expect("could not relay text");
}
server.send_with_str("text").expect("could not relay text");
}
WebsocketEvent::Close(_) => {
console_log!("Connection closed")
}
}
}
});
Response::from_websocket(pair.client)
})
.run(req, env)
.await
2. then make a request, using postman "{current-url}/ws-client"
3. Crash occurs
Contributor guide
Assessment
This issue has not been assessed yet.