There is an example in the mio
crate that claims to show there is limited support for wasm
from two years ago. https://github.com/tokio-rs/mio/blob/master/examples/tcp_listenfd_server.rs
The file has the following comments:
// You can run this example from the root of the mio repo:
// cargo run --example tcp_listenfd_server --features="os-poll net"
// or with wasi:
// cargo +nightly build --target wasm32-wasi --example tcp_listenfd_server --features="os-poll net"
// wasmtime run --tcplisten 127.0.0.1:9000 --env 'LISTEN_FDS=1' target/wasm32-wasi/debug/examples/tcp_listenfd_server.wasm
But the instructions seem out of date. First it doesn't compile because some warnings are treated as errors. But if that is worked around by allowing warnings so they are not treated as errors, the call to wasmtime
fails because the args have changed.
The best I could come up with is this command, but it fails too.
$ wasmtime -S tcplisten=127.0.0.1:9000 --env 'LISTEN_FDS=1' target/wasm32-wasi/debug/examples/tcp_listenfd_server.wasm
Error: components do not support --tcplisten
That mio example is pretty old yeah and is built on outdated support for sockets in WASI. More-or-less the mio support needs to be updated/rewritten to use wasip2 instead of wasip1
If I can get the -S tcplisten arg to work again, I will send a PR to mio with what I find. Just today someone else is trying to use that argument with a fresh tokio wasi build.
This is my cue to spam everybody with my mio
/tokio
WASIp2 proof of concept: https://github.com/dicej/wasi-sockets-tests
Found the new incantation (as long as mio
is still being built against wasi
0.11.0).
cargo build --target wasm32-wasi --example tcp_listenfd_server --features="os-poll net"
wasmtime run -S preview2=n -S tcplisten=127.0.0.1:9000 --env 'LISTEN_FDS=1' ./target/wasm32-wasi/debug/examples/tcp_listenfd_server.wasm
It's a neat little demo of a single threaded TCP listener able to write to any nc
that connections to it, and echo to stdout anything that the nc
endpoints may send to it. I tried with two nc
connected at the same time.
Frank Rehwinkel has marked this topic as resolved.
Last updated: Nov 22 2024 at 16:03 UTC