Java Reference
In-Depth Information
What we are interested in here is the embedded JavaScript code between the
<script> tags. You can see that the send_echo() JavaScript function creates a new
JavaScript WebSocket object then assigns the onopen , onmessage, and onerror
functions of the WebSocket object to the onOpen() , onMessage(), and onError()
functions that are embedded in the code. Now, our onOpen() function will be
automatically invoked when a WebSocket connection is opened, our onMessage()
function will be called whenever a WebSocket message is received from the server,
and our onError() function will be automatically called whenever there is a
WebSocket error.
The Echo sample project simply updates the div with the ID of output with the
message received from the server. This is done in the writeToScreen() function,
which is invoked from the onMessage() function whenever a WebSocket message
is received from the server.
The Echo sample project is a great way to understand how to write our own
applications using the Java API for WebSocket. In the next section, we will write our
own application, shamelessly borrowing as much as possible from the sample Echo
application to make our job easier.
Building our own WebSocket
In the previous section, we saw how NetBeans provides sample WebSocket
applications we can use as a base for our own projects. In this section, we will build a
web application that contains a WebSocket server endpoint that will populate a form
with default values.
Search WWH ::

Custom Search