Basic WebSocket Server
This app demonstrates a WebSocket server running on the device.
- SDK directory:
applications/gecko_os/basic/websocket_server
API Features Demonstrated
Platforms
This app works on:
- WGM160P
Requirements and Prerequisites
Requires a Gecko OS evaluation board, such as a WGM160P_eval.
Other Gecko OS hardware can also be used by changing the source of the logged data to suit your device.
Resources
The initialization file settings.ini is packaged into the App 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 Gecko OS serial terminal to the device. See Getting Started, Opening a Gecko OS Terminal.
This app assumes your Gecko OS device is set up with the credentials to join your local network. If you have not already set up network credentials, on the Gecko OS 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
gos_app_init
gos_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
.gos_network_restart brings the network WLAN interface down and up again so that the HTTP server amd mDNS service start.
gos_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:
- gos_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 Gecko OS terminal when the network goes down or comes up.
websocket_connect_handler
- when a client connects, gos_websocket_get_client_info gets the client IP and port information, and logs a message to the Gecko OS terminal.
websocket_disconnect_handler
- This logs a message when a client disconnects.
websocket_receive_handler
- gos_websocket_read reads data from a WebSocket stream when available.
- The data is logged to the Gecko OS terminal
- gos_websocket_write echoes data back to the WebSocket stream.
Source
See: