kettle11 / devserver

A simple HTTPS server for local development. Implemented in Rust.
zlib License
95 stars 15 forks source link

How to send large chunks of data to connected clients #5

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi! I came across an issue where send_websocket_message is invoked and a message is sent to the connected clients. I tried sending path.to_path_buf().to_str().unwrap() and it works. However, when I read the file's content (12 lines of HTML), converting it to a slice and sending it, I get the following error in the browser:

WebSocket connection to 'ws://localhost:8129/' failed: Invalid frame header

Tools

Browser: Chrome 81.0.4044.138 (Official Build) (64-bit) Platform: Windows 10 OS Version 1903 (Build 18362.778) Rustup toolchain: nightly-x86_64-pc-windows-msvc (default & active)

kettle11 commented 4 years ago

Thanks for the report @undefinedbuddy

It's likely send_websocket_message was not fully tested and may not work. devserver only uses it to send an empty message, so `send_websocket_message' is mostly unused.

I intended to eventually implement reloading certain individual files without a full refresh of the page, but I never got around to it so send_websocket_message is just used to signal the page to refresh but does not send any actual data.

If you're looking to repurpose that function for other uses it may need a little fixing up to get working, I don't remember what issues were outstanding with it.

ghost commented 4 years ago

Great! I'm actually kinda stuck with a large codebase where this functionality is quite necessary. Any pointers would sure help.

Thanks for the heads up.

kettle11 commented 4 years ago

I don't know the details of your project, but if you need something like devserver but with the ability to refresh individual files without reloading the page then I won't personally be adding that to devserver soon.

I considered it, which is why send_websocket_message exists, but it's actually a rather tricky problem that requires more thought. The current solution is fine for the majority of use cases.

If you need a custom way to send data from a server to a web page with a WebSocket do not use devserver's implementation. In order to keep devserver extremely lightweight only a tiny portion of the WebSocket protocol was implemented, but not enough to use it for other purposes.

I just saw a reddit post today discussing WebSocket crates which may be useful to you: https://www.reddit.com/r/rust/comments/goxm85/which_websocket_library_to_use/

Hope this helps!

ghost commented 4 years ago

Totally agreed. I'll consider the link. Thanks for the help.