Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 1.2 KB

README.md

File metadata and controls

32 lines (25 loc) · 1.2 KB

Emscripten TCP example

Experimental Preview

git clone https://github.com/cloudflare/workers-rs
cd workers-rs/examples/emscripten-tcp
npx wrangler dev

Raw TCP from a Worker with tokio::net, using the Emscripten Tokio patchset (the guybedford/tokio tag pinned in Cargo.toml, tokio-rs/tokio#8438). Building on the emscripten-tokio example, the Worker connects to port 80 of a host, sends an HTTP HEAD request and returns the reply.

npx wrangler dev
curl 'http://localhost:8787/?host=example.com'

TcpStream::connect((host, 80)), write_all and read_to_string are the ordinary Tokio API, under a tokio::time::timeout. The socket is backed by the Workers connect() API through Emscripten's -sNODERAWSOCKETS, and readiness flows through the same event loop that drives the handler. The hostname resolves through Emscripten's asynchronous getaddrinfo (emscripten#27742, applied by worker-build), which Tokio's ToSocketAddrs uses on this target.

What's Next

See the emscripten-incoming-tcp example for using a native accept to receive a TCP connection.