sammhicks / picoserve

An async no_std HTTP server suitable for bare-metal environments, heavily inspired by axum
MIT License
195 stars 23 forks source link

Websocket on embassy #17

Closed brychanrobot closed 8 months ago

brychanrobot commented 9 months ago

Is websocket expected to work on embassy? I tried to set them up working from https://github.com/sammhicks/picoserve/blob/main/examples/web_sockets/src/main.rs, and the requests just fail on the browser side. I'm not sure why that would be. Other requests for / and others work fine, but when I try to create a new WebSocket() in the js, the GET request never even shows up on the microcontroller.

The very reduced route for /ws just to see if the GET request is even coming in.

.route("/ws", get(|| async move {
                println!("requested websocet");
                "stuff"
            }));

The WebSocket constructor in js. This is run from the index.html|js that are hosted on the microcontroller.

let ws = new WebSocket("ws://" + window.location.host + "/ws", ["messages"]);

Chrome console error message index.js:66 WebSocket connection to 'ws://test-hostname.lan:8000/ws' failed:

Firefox console error message Firefox can’t establish a connection to the server at ws://test-hostname:8000/ws

brychanrobot commented 9 months ago

Update. I did get it to connect. It seems to always fail the first attempt and succeed on the second. Not sure why that is. Maybe my web task pool is too small so it's just getting dropped?

sammhicks commented 9 months ago

I've added a feature in version 0.6.0 where you can configure if the TCP connection is closed or kept alive after the response has been sent. Does closing the connection after the response has been sent solve the problem?

hhirvola commented 8 months ago

I have the same problem. Embassy based RP2040 project I have been playing with. Websockets work fine in Picoserve up to version 0.5. With Picoserve 0.6 creating websocket always fails. Keepalive does not seem to affect things.

sammhicks commented 8 months ago

Websockets work fine in Picoserve up to version 0.5. With Picoserve 0.6 creating websocket always fails. Keepalive does not seem to affect things.

Thanks for the additional info, I think that I've found the problem. In picoserve 0.6, a websocket response has a repeated "Connection" header, one with "keep-alive", and the other with "upgrade", which causes problems. I've put a fix on the branch "fix-websockets", are you OK to test it? If it still doesn't work, are you OK to send me the code (or a link to it) so that I can test things at my end?

Thanks all for the help, much appreciated :D

hhirvola commented 8 months ago

Seems that "fix-websockets" solves the issue. I only had time for quick test but web socket creation succeeded both with and without keepalive.

sammhicks commented 8 months ago

I've now merged "fix-websockets" into main