I want to write a webserver using Rust, Hyper and websocket-rs. The webserver must be able to handle http requests AND websocket requests on the same port. I used the official sample (async-server.rs: https://github.com/cyderize/rust-websocket/blob/master/examples/async-server.rs) and tried to modify it. My idea was to change the error handling. If the client's request is a simple http request then do not return an error, but spawn a future which handles the request instead.
Based on another SO question (How do I handle an error when using rust-websocket so that only that connection fails and not the entire program?) I changed the error handling from the sample.
This is the old code:
.map_err(|InvalidConnection {error, ..}| {
println!("Error:{:?}",error);
return error;
})
Here is my new code:
.map(Some).or_else(|_| -> Result<_, ()> {
// SPAWN THE FUTURE
Ok(None)
})
This is where I am stuck. The sample code calls a function spawn_future, which expects a parameter (upgrade.reject()) which is not available in my code since I do not have access to an upgrade structure. I tried a few other things (i.e. calling handle.spawn_fn) but the compiler never liked what I did. (I could paste some of the compiler errors, but I do not think that they would improve the quality of my post.)
Here is my question: What is the recommended way to write an asynchronous server which can handle http requests AND websocket requests on the same port? If my approach is correct: can you please help me to fill the SPAWN THE FUTURE line?
Additional context (if you haven't noticed yet): I am a total Rust beginner.