| By adding a @(Base)WebSocketBehavior@ to your component(s) Wicket will contribute @wicket-websocket-jquery.js@ library which provides some helper functions to write your client side code. There is a default websocket connection per Wicket Page opened for you which you can use like: |
| {code} |
| Wicket.WebSocket.send('{msg: "my message"}'). |
| {code} |
| |
| If you need more WebSocket connections then you can do: |
| {code} |
| var ws = new Wicket.WebSocket(); |
| ws.send('message'); |
| {code} |
| |
| To close a connection: |
| {code} |
| Wicket.WebSocket.close() |
| {code} |
| |
| or |
| {code} |
| ws.close() |
| {code} |
| |
| Wicket.WebSocket is a simple wrapper around the native window.WebSocket API which is used to intercept the calls and to fire special JavaScript events (Wicket.Event PubSub). |
| Once a page that contributes @(Base)WebSocketBehavior@ is rendered the client may react on messages pushed by the server by subscribing to the @'/websocket/message'@ event: |
| |
| {code} |
| Wicket.Event.subscribe("/websocket/message", function(jqEvent, message) { |
| var data = JSON.parse(message); |
| processData(data); // does something with the pushed message |
| }); |
| {code} |
| |
| Here is a table of all events that the application can subscribe to: |
| {table} |
| Event name | Arguments | Description |
| /websocket/open | jqEvent | A WebSocket connection has been just opened |
| /websocket/message | jqEvent, message | A message has been received from the server |
| /websocket/closed | jqEvent | A WebSocket connection has been closed |
| /websocket/error | jqEvent | An error occurred in the communication. The connection will be closed |
| {table} |
| |