Basic WebSocket Server
This app demonstrates a WebSocket server running on the device.
- SDK directory:
apps/basic/websocket_server
- Zentri App Store:
ZENTRI-BAWEBSOCSERV
API Features Demonstrated
Platforms
This app works on:
- AMW004 - Zentri Mackerel evaluation board
- AMW106 - Zentri Moray evaluation board
- NXP-SCCK - NXP Secure Cloud Connectivity Kit
Requirements and Prerequisites
This app requires a ZentriOS evaluation board, such as an AMWx06-E03 (Moray).
Resources
The initialization file settings.ini is packaged into the .zap
file downloaded to the device.
The file websocket_server_demo.html
is downloaded to the device.
Description
This app demonstrates a WebSocket server running on the device.
The device also runs an HTTP Server that serves out a web page to a client system.
On the client system, entering a message in the demonstration web page Message
text box sends the message to the server, which then echoes the result back to the client. The web page displays the echoed message in the Message Log
text area.
Instructions for Use
Open a ZentriOS serial terminal to the device. See Getting Started, Opening a ZentriOS Terminal.
This app assumes your ZentriOS device is set up with the credentials to join your local network. If you have not already set up network credentials, on the ZentriOS terminal, run the commands:
network_up -s
save
See the Wi-Fi Command API documentation for network_up and save.
After compiling, downloading and running the app, wait until the device is connected to the local network.
Then in a web browser on a computer or mobile device connected to the same network, open the URL http://websocket_server.local/
.
On the WebSocket Demonstration page, in the Device IP or Name
text box, specify the name websocket_server
and click Open WebSocket
When the Session Log
text area displays WebSocket open...
, type a message in the Message
text box and press Enter
.
In the Message Log
text area, the messages you send from the WebSocket client are shown in orange. Messages echoed from the WebSocket server are shown in blue.
Implementation
zn_app_init
zn_load_app_settings
("settings.ini")
loads settings.ini. The settings start up the HTTP server when the network comes up, setwebsocket_server_demo.html
as the root web page, and set up the mDNS namewebsocket_server
.zn_network_restart brings the network WLAN interface down and up again so that the HTTP server amd mDNS service start.
zn_websocket_register_server_event_handlers sets up the event handlers:
- connect WebSocket:
websocket_connect_handler
- disconnect WebSocket:
websocket_disconnect_handler
- data available from client:
websocket_receive_handler
- connect WebSocket:
- zn_network_register_event_handler sets up
wlan_network_event_handler
to handle network connect/disconnect events.
wlan_network_event_handler
- This logs a message to the ZentriOS terminal when the network goes down or comes up.
websocket_connect_handler
- when a client connects, zn_websocket_get_info gets the client IP and port information, and logs a message to the ZentriOS terminal.
websocket_disconnect_handler
- This logs a message when a client disconnects.
websocket_receive_handler
- zn_websocket_read reads data from a WebSocket stream when available.
- The data is logged to the ZentriOS terminal
- zn_websocket_write echoes data back to the WebSocket stream.
Source
See:
See also: