Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
en:netzer:websockets [2013/06/04 15:41] – Specific commands and limitations added svesch | en:netzer:websockets [2025/06/11 20:42] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== WebSocket ====== | ||
+ | WebSocket is a protocol for bidirectional communication between a web application and a server via a TCP connection.\\ | ||
+ | Netzer uses WebSocket as a channel for the [[commandinterface|command interface]]. | ||
+ | |||
+ | |||
+ | ====== Why WebSocket? ====== | ||
+ | |||
+ | In normal HTTP a client (i.e. the browser) sends a request to a server. After some processing time the server responses to the client. This is one way, the server can not serve files or data by itself without been requested before. | ||
+ | This is a disadvantage i.e. if data must be asynchronously refreshed. In such cases the client has to poll the server which leads to a lot of traffic. | ||
+ | |||
+ | WebSocket are a possibilty for bidirectional communication between server and client. Client and server can send data on demand - Both are equal communication partners. While refreshing data it is enough to send data as soon they are available. It is not necessary anymore to poll the server. | ||
+ | |||
+ | <note important> | ||
+ | Netzer supports Websocket since Version 1.5! | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====== Limitations ====== | ||
+ | |||
+ | Because an active WebSocket connection needs an active TCP connection to the web server of Netzer the further access of web pages is blocked. | ||
+ | Also only one simultaneous WebSocket connection is possible. | ||
+ | |||
+ | |||
+ | ===== Supported commands ===== | ||
+ | |||
+ | All commands of the [[commandinterface|command interface]] are supported. | ||
+ | There are no WebSocket specific commands. | ||
+ | |||
+ | ====== WebSocket URI ====== | ||
+ | |||
+ | A WebSocket URI commonly starts with '' | ||
+ | |||
+ | The WebSocket URI of Netzer follows the schema '' | ||
+ | The WebSocket of the Netzer with IP address '' | ||
+ | |||
+ | |||
+ | ====== WebSockets in JavaScript ====== | ||
+ | |||
+ | ===== Open connection ===== | ||
+ | |||
+ | A new WebSocket connecion can be opened with creating a new websocket object. | ||
+ | |||
+ | < | ||
+ | |||
+ | In some Firefox versions (till version 11) the WebSocket object has the name " | ||
+ | |||
+ | < | ||
+ | |||
+ | |||
+ | ===== Send data ===== | ||
+ | |||
+ | WebSockets support two fundamental transmission types: Text (UTF-8) and binary. Data is send with '' | ||
+ | |||
+ | Netzer currently only supports text transmissions, | ||
+ | |||
+ | The text transmission type is only used, if the parameter for '' | ||
+ | |||
+ | < | ||
+ | myWebSocket.send(myData);</ | ||
+ | |||
+ | |||
+ | ===== Receive data ===== | ||
+ | |||
+ | Receiving data is handled via an event handler '' | ||
+ | < | ||
+ | |||
+ | |||
+ | ===== Close connection ===== | ||
+ | |||
+ | To close the WebSocket connection, the function '' | ||
+ | |||
+ | < | ||
+ | |||
+ | |||
+ | ===== Further event handler ===== | ||
+ | |||
+ | ==== onopen ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | |||
+ | ==== onclose ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | < | ||
+ | |||
+ | |||
+ | ==== onerror ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | |||
+ | ===== A simple example ===== | ||
+ | |||
+ | Showing the current value of the Netzer pin IO0. | ||
+ | <WRAP center round download 60%> | ||
+ | [[@/ | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <meta http-equiv=" | ||
+ | < | ||
+ | var myWebSocket, | ||
+ | // verbinden | ||
+ | connect = function() { | ||
+ | // Clean up old websocket connection. | ||
+ | if(myWebSocket) | ||
+ | { | ||
+ | myWebSocket.close(); | ||
+ | } | ||
+ | |||
+ | // Extract URL | ||
+ | url = document.getElementById(" | ||
+ | |||
+ | // Establish new connection. | ||
+ | if(" | ||
+ | { | ||
+ | myWebSocket = new WebSocket(url); | ||
+ | } | ||
+ | else if(" | ||
+ | { | ||
+ | myWebSocket = new MozWebSocket(url); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | alert(" | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // Set handler for incoming messages. | ||
+ | myWebSocket.onmessage = receiveMessage; | ||
+ | |||
+ | // If the connection is established, | ||
+ | myWebSocket.onopen = function() {myWebSocket.send(" | ||
+ | }, | ||
+ | // Receive Message | ||
+ | receiveMessage = function(event) { | ||
+ | msgJSON = JSON.parse(event.data); | ||
+ | |||
+ | // Is it an update of the value IO0? | ||
+ | if(msgJSON && msgJSON.u && msgJSON.u.v && msgJSON.u.v[" | ||
+ | { | ||
+ | document.getElementById(" | ||
+ | } | ||
+ | // Ignore other messages. | ||
+ | }, | ||
+ | // Function for initializing page. | ||
+ | initPage = function() { | ||
+ | // Delete javascript hint. | ||
+ | document.getElementById(" | ||
+ | // Set event handler | ||
+ | document.getElementById(" | ||
+ | }; | ||
+ | </ | ||
+ | </ | ||
+ | <body onload=initPage()> | ||
+ | <div id=" | ||
+ | <input value=" | ||
+ | <input id=" | ||
+ | < | ||
+ | <div style=" | ||
+ | </ | ||
+ | </ | ||
+ | </ |