Skip to main content

websocket-frame

:::warning [UNTESTED] — and the real library is not linked Two separate caveats. First, the usual one: native_sim only, no bench. Second: libwebsockets is not linked here. This example hand-rolls the RFC 6455 frame it demonstrates. See Integration reality. :::

Mask, encode, and decode a single unfragmented RFC 6455 TEXT frame — offline, in a buffer.

Source: examples/connectivity/websocket-frame/.

What it does

ws_frame_encode_text() masks and encodes one TEXT frame into a buffer using a fixed demo mask key; ws_frame_decode_text() decodes it back and the payload is asserted. The point is proving the byte layout before it ever touches a TCP socket.

A WebSocket TEXT frame here is 2 header bytes + a 4-byte mask key + the payload.

Integration reality

The libwebsockets module does fetch (~230 KB of source at modules/lib/libwebsockets), but it cannot be used offline as-is:

  • It ships no zephyr/module.yml.
  • Its public header unconditionally includes an lws_config.h that only exists after running libwebsockets' own CMake configure step — out of scope for a small teaching example.
  • lws_write() needs a live wsi plus an event loop, so there is no offline framing entry point to call.

libraries: [libwebsockets] emits CONFIG_ALP_LWS_NO_TLS=y (the software-fallback default) but does not add the module's include path or build its sources.

Hardware swap

No board-specific wiring — pure buffer logic. Wired to a real lws_context over a TCP/TLS socket, the transport is what changes; the frame layout this example teaches stays the same regardless of SoM family.

board.yaml

libraries:
- name: libwebsockets
cores: [m55_hp]

som:
sku: E1M-AEN801

preset: e1m-evk

cores:
m55_hp:
app: ./src
peripherals: []

diagnostics:
log_level: info

Expected output

[websocket-frame] encoding TEXT frame: payload="hello lws" (9 bytes)
[websocket-frame] encoded 15 bytes (2 header + 4 mask key + 9 payload)
[websocket-frame] decoded payload="hello lws" (9 bytes)
[websocket-frame] round trip OK
[websocket-frame] done

See also

Questions about this page? Discuss in Community Forum