GitHub - hyperlight-dev/hyperlight-wasm-sockets-example (original) (raw)
hyperlight-wasm
sockets example
This is a minimal example of aHyperlight-Wasmhost application. It implements just enough of the wasi:sockets
UDP api (in an unfortunately blocking fashion) to run the echo sample server.
Prerequisites
- Rust, including the
x86_64-unknown-none
target (which may be installed via e.g.rustup target add x86_64-unknown-none
) - A C compiler
- wasm-tools
- If you are fetching the sample binary from an OCI registry,wkg.
Building
Compile the WIT and set the environment variables used when building (both the host and the guest):
wasm-tools component wit hyperlight.wit -w -o hyperlight-world.wasm export HYPERLIGHT_WASM_WORLD=$PWD/hyperlight-world.wasm
Build:
Running
Get an echo.wasm
from the sample repo, either by building it or by fetching it from the OCI registry (wkg oci pull ghcr.io/hyperlight-dev/wasm-udp-echo-sample/udp-echo-server:latest -o echo.wasm
).
AOT compile it:
cargo install --git https://github.com/hyperlight-dev/hyperlight-wasm hyperlight-wasm-aot hyperlight-wasm-aot compile --component echo.wasm echo.bin
You can then run the server:
cargo run # or target/debug/echo
This will not produce any output on stdout, and should wait forever to receive UDP packets on 127.0.0.1:8080
.
Testing
In another shell:
Linux
When using interactively, due to line-buffering, you will likely need to send an entire line to nc
before receiving a response.
Windows
Windows does not have a native nc
command, however we can use a PowerShell function to achieve the same result:
Create a PowerShell function to send UDP messages
. ./scripts/Send-UDPMessage.ps1
Run the function to send a message
Send-UDPMessage -IPAddress "127.0.0.1" -Port 8080