In Part-1
we got our hands dirty with a simple WebSockets
example.
For each new client connection, a separate new instance of the server
endpoint class com.polarsparc.websockets.SimpleMonitor
is created.
Is it possible to create a single instance of the server endpoint class
and manage the client sessions within ???
In this part, we will do just that by creating a custom factory by
extending the class javax.websocket.server.ServerEndpointConfig.Configurator.
Hands-on with WebSockets - 2
We will demonstrate the use of ServerEndpointConfig.Configurator
to create a single instance of the server endpoint class for all of the
client connections with another simple monitoring example that will
display the Memory Total and Free space.
Note that this example will work only on the Linux platforms.
The following is the custom ServerEndpointConfig.Configurator
Java code that will create a single instance of the server endpoint
class com.polarsparc.websockets.SimpleMonitor2
and reuse it for each of the client connections:
The java class javax.websocket.server.ServerEndpointConfig.Configurator
can be extended to create a custom configurator which can then be
applied to a server endpoint to modify the default configuration
behavior. In our example, we use it to create a single instance of the
server endpoint class and associate it with each client that connects.
The method getEndpointInstance() is
invoked by the container each time a new client connects to the server
endpoint. This method returns an instance of the server endpoint that
will handle all the interactions from the client.
The following is the server-side Java code that will push the Memory
metrics at a regular interval of 5 seconds:
We will highlight some of the aspects from the source code in Listing.2
above as follows:
Notice how the annotation @ServerEndpoint
defines the server endpoint using the value
attribute and the custom server endpoint configurator java class
using the configurator attribute.
The annotation @OnClose can be
applied to a method that takes two arguments: a javax.websocket.Session
and a javax.websocket.CloseReason.
The class CloseReason encapsulates the
reason (code and text) why a WebSocket
between a client and the server was closed.
The annotation @OnMessage can be
applied to a method that takes two arguments: a java.lang.String
and a javax.websocket.Session.
The annotation @OnError can be
applied to a method that takes two arguments: a javax.websocket.Session
and a java.lang.Throwable.
The following is the client-side HTML/JavaScript
code that initializes and uses a WebSocket
connection:
Now that we have the code for both the WebSocket
server and client, we need to perform some setup as follows:
Copy the java classes of the server SimpleMonitor2
under the directory $CATALINA_HOME/webapps/polarsparc/WEB-INF/classes/com/polarsparc/websockets
Copy the html file of the client SimpleMonitor2.html
under the directory $CATALINA_HOME/webapps/polarsparc
Start the Tomcat 8.x server
Now launch two separate instances of the Firefox
browser and enter the following URL in both the browser instances: