HTTP Server Simple WebSocket Demonstration
ws://deviceName/stream that causes the device to open a WebSocket. For more on the WebSocket API, see Mozilla WebSocket API.
With the socket open, you can send and receive messages between the client and the WiConnect device.
At the WiConnect device end, in command mode, you can read messages with stream_read and send messages with stream_write. In stream mode, data streams directly to and from the WebSocket.
Open a WiConnect terminal.
Configure WiConnect Device
Configure as follows:
Note:The network.buffer.rxtx_ratio setting is necessary only for the Numbat, which has a smaller memory. See Memory.
After rebooting, the WiConnect Terminal displays messages similar to:
[Associating to ackme] > Obtaining IPv4 address via DHCP IPv4 address: 10.5.6.55 Starting mDNS mDNS domain: mymodule.local HTTP and REST API server listening on port: 80 [Associated]
Download and Launch the HTML page
Download the HTTP Server Simple WebSocket Demonstration web page: http_server_ws_simple.html.
Run this page in a web browser to act as the client. You can run it as a
The web page has input boxes for the device IP or name, and for a message to send from the client.
By default, the WiConnect device bus mode is
command. You issue commands to read from and write to the WebSocket.
In the web page
Device IP or Name input box, specify the mDNS name or the IP address of the module. If you used the command sequence above, the mDNS name is the default,
Open WebSocket button to open the WebSocket.
The web page session log indicates a successful connection.
In the WiConnect Terminal, WiConnect indicates a connection has been opened with a response similar to:
> [2015-02-12 | 06:12:09: Opened: 0]
The stream_list command displays a list of open streams, similar to:
> list ! # Type Info # 0 WEBS 10.5.6.55:80 10.5.6.60:63745
In the web page, enter a message in the
Message input box and press Enter to send the message.
In the WiConnect Terminal, view the message by issuing the command:
read 0 1000
The first argument is the stream id, and should be modified depending on the WebSocket stream. See stream_read. The response is similar to:
To send a message back to the web page via the WebSockets, in the WiConnect Terminal issue the command:
write 0 5 Hello
Press Enter after typing 'write 0 5', then type the text of the message,
hello. The first argument is the stream id, and should be modified depending on the WebSocket stream. See stream_write.
The web page message log shows the message received.
Now, in the WiConnect Terminal, switch to stream mode:
set bus.mode stream save reboot
The WiConnect device reboots in stream mode and displays messages indicating the the HTTP server and mDNS are running.
In the web page, open a new WebSocket.
When the WebSocket opens successfully, send a message to the device from the web page. It appears immediately on the WiConnect Terminal console.
Type a message on the WiConnect Terminal console. Depending on terminal echo settings, it may not appear on the terminal, but each character you type is streamed directly to the the web page message log, via the WebSocket connection.
The WebSocket is initialized in the
initWebSocket() function, with the line:
ws = new WebSocket('ws://'+ipName+'/stream')
In the same function, calls to the WebSocket
onmessage functions set callbacks for those events.
sendMessage function, a call to the WebSocket
send method sends the message to the device.