Real time bidirectional communication between Vizard and a web browser is supported using WebSockets. You can send and receive messages and find out when socket connections open or close. On the server (Vizard) side, the vizhtml module provides Vizard commands for sending messages and handling message and connection events. Similar functionality is provided on the client (browser) side using JavaScript functions through vizhtml's JavaScript library.
All the major web browsers support WebSockets. The minimum supported version for each browser is:
Use the following commands to send WebSocket messages and get information about connected clients:
Command |
Description |
vizhtml.sendAll(event, data) |
Sends WebSocket data to all connected clients.
event: A string value that defines the event name.
data: Any basic object type (e.g. int, string , list, ...) or a viz.Data object. |
vizhtml.getClientList() |
Gets a list containing the currently connected clients. Each element in the list is an instance of the vizhtml.WebSocketClient class. |
vizhtml.getClientCount() |
Returns the number of currently connected clients. |
Sending data to a specific client is accomplished through it's associated vizhtml.WebSocketClient object. You can get a handle to this object when a WebSocket event occurs (e.g. connection event) or by using the vizhtml.getClientList() command. Each object provides the client's IP address and a method to send it data:
Attribute |
Description |
<WebSocketClient>.address |
The IP address of the client. |
Method | Description |
<WebSocketClient>.send(event, data) | Sends WebSocket data to the client.
event: A string value that defines the event name.
data: Any basic object type (e.g. int, string , list, ...) or a viz.Data object. |
The following events will get triggered when messages are received and connections are opened and closed:
Event | Description |
vizhtml.MESSAGE_EVENT | Generated when a WebSocket message is received. It provides a single event structure e with the following attributes: e.event: A string with the event name. This should match an event_name defined in a <VizardWebSocket>.send command (described in the JavaScript section below). e.data: The data sent. e.client: A vizhtml.WebSocketClient object. |
vizhtml.CONNECT_EVENT | Generated when a WebSocket client connects. It provides a single event structure e with the following attribute:
e.client: A vizhtml.WebSocketClient object. |
vizhtml.DISCONNECT_EVENT | Generated when a WebSocket client disconnects. It provides a single event structure e with the following attribute:
e.client: A vizhtml.WebSocketClient object. |
Use the following commands to register callback functions for these events:
Command | Description |
vizhtml.onMessage(event, func, *args, **kw) | Registers a callback function for a message event:
event: A string with the event name. This should match an event_name defined in a <VizardWebSocket>.send command (described in the JavaScript section below).
func: The function being registered. An event object e, described in the table above (see vizhtml.MESSAGE_EVENT) will be passed to the callback function.
*args: Optional arguments to pass to the registered function.
**kw: Optional keyword arguments to pass to the registered function. |
vizhtml.onConnect(func, *args, **kw) | Registers a callback function for a WebSocket connection event:
func: The function being registered. An event object e, described in the table above (see vizhtml.CONNECT_EVENT) will be passed to the callback function.
*args: Optional arguments to pass to the registered function.
**kw: Optional keyword arguments to pass to the registered function. |
vizhtml.onDisconnect(func, *args, **kw) | Registers a callback function for WebSocket disconnection event:
func: The function being registered. An event object e, described in the table above (see vizhtml.DISCONNECT_EVENT) will be passed to the callback function.
*args: Optional arguments to pass to the registered function.
**kw: Optional keyword arguments to pass to the registered function. |
Use vizhtml's JavaScript library to create a WebSocket client that communicates with vizhtml's HTTP server. In order to access the library within your own JavaScript code, link to vizhtml.js as an external script:
To implement a WebSocket client create an instance of the viz.WebSocket class. Before creating a WebSocket, you can check whether the browser supports WebSockets by calling the viz.isWebSocketSupported() command. This provides a way to display a helpful error message to the user.
The following methods are available on the viz.WebSocket object:
Method | Description |
<WebSocket>.onevent(event_name, func) | Registers a callback function for an event.
event_name: A string with the event name. For message events, this argument should match an event value defined in one of the server commands (either vizhtml.sendAll or <WebSocketClient>.send). Otherwise, see the table below for connection event values.
func: The JavaScript function being registered. An event object e with the following attributes will be passed to the callback function:
|
<WebSocket>.send(event_name, event_data) | Sends data to the WebSocket server.
event_name: A string value that defines the event name.
event_data: Any basic object type (e.g. int, string , array, ...) or the return object from the viz.Data function. |
The table below lists event_name values for connection events,
Value |
'open' |
'close' |
'error' |
If you want the JavaScript code to send an object that appears to Vizard as a viz.Data object, then you can use the viz.Data JavaScript function:
Command | Description |
viz.Data(data) | Returns an object that will be converted to a viz.Data object when sent to the vizhtml server.
data: a JavaScript associative array that uses strings as keys. |
In the following JavaScript code, the integer value stored in click_count is sent as the count attribute of a viz.Data object:
In Vizard, the following code retrieves and prints the value:
The following example script shows how to communicate between Vizard and a browser via WebSockets.
Note: If no communication occurs it's likely your browser does not support WebSockets.
For testing on a local browser use 'localhost' or the loopback address:
http://localhost:8080/vizhtml/websocket
http://127.0.0.1:8080/vizhtml/websocket
For remote browsers replace computer name or IP address with the Vizard machine's name or address
http://computer name:8080/vizhtml/websocket
http://IP Address:8080/vizhtml/websocket