How to get TcpStream to work in WASM?


#1

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:

  1. 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.
  2. Use websocket or some other crate for WebSockets: tried websocket, ws and some others, but I could not get any of these to compile.

Since there is not yet any good documentation on wasm in general, I have come here to you to look for the solution to this problem. Should I just use JavaScript and be happy with it? Is it my lack of knowledge of the front end? How does the standard library handle TCP/UDP in wasm, where it seems there is no true access to a pure transport layer connection?

Help!


#2

It sounds like the TcpStream is running in a nonblocking mode. It looks like emscriptn/wasm support epoll, so mio and tokio might be ways to go there.


#3

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.

Well, at least that is what Github exists for…