Hello! I have been trying for some time to open a TCP (or UDP, WebSocket,… ) connection from Rust compiled to Web Assembly without any luck. These are the two approaches I have tried:
Use std: use the built-in implementations available in the standard library. Unfortunately, I always get OS error 115 (Operation in progress) when I do unwrap after TcpStream::connect. In the Firefox console, this appears as a failing WebSocket.
Use websocket or some other crate for WebSockets: tried websocket, ws and some others, but I could not get any of these to compile.
Thanks! I will give this a shot. Though I have taken a look at mio and it currently does not support emscripten. It does not complie at all. I will have to tinker with the source code to make it work, most probably… maybe add some cfg(target_os = "emscripten") here and there.
For Rust you should be using the Emscripten target to benefit from any of this (IIRC it defers to the Linux implementation in most cases, which is what you want). It’s not the end of the world to implement your own interface accessible from the WASM target, but it is extra effort.
Onto the issues - TcpStream::connect won’t work (as you’ve noticed) because of EINPROGRESS. You may end up having success with TcpStream::connect_timeout, as it connects in nonblocking mode and then uses poll to check if you’re successful.
Alternatively, use the libc crate to do your socket + connect dance, ignore EINPROGRESS and then use FromRawFd to create your new TcpStream from the file descriptor.
Once you have the TcpStream it should work pretty well in non-blocking mode.