Java Reference
In-Depth Information
In the original Echo sample application, a new WebSocket connection was created
every time we clicked on the button labeled Press me . We modified the JavaScript
code to establish the connection only once when the page loads for the first time. We
added the necessary calls to the init() JavaScript function. In this function, we bind
some of our JavaScript functions to react to certain WebSocket events. Our onOpen()
function will be called when a connection is made to our WebSocket server endpoint.
The onMessage() function will be invoked when the client receives a message from
the WebSocket server endpoint, and onError() will be invoked if there is an error
while communicating with the WebSocket server endpoint.
Our onOpen() and onError() JavaScript functions are slightly modified versions of
the corresponding functions in the Echo sample message; in our case, we modified
them to simply display a message on the browser log.
In most browsers, the browser console can be seen by hitting F12 and
clicking on the Console tab.
Our onMessage() function parses the JSON string sent by our WebSocket server
endpoint and populates our form with the appropriate values.
As far as the actual markup goes, we added a button labeled Get Defaults that
invokes our doSend() JavaScript function, passing the get_defaults string as a
parameter. The doSend() function, in turn, passes this string to our WebSocket
server endpoint via the send() function of the JavaScript WebSocket object. Our
WebSocket server endpoints returns a JSON string with default values when it
gets this exact string as a parameter.
The following screenshot shows our application in action:
Search WWH ::

Custom Search