Implementing a Java Server Side component for jWebSocket Server for WebSocket interaction with Web Clients

In a previous post – https://technology.amis.nl/blog/14949/push-based-synchronized-slideshow-web-application-implemented-using-websockets-and-jwebsocket-server-the-open-source-java-websocket-server – I discussed the implementation of the synchronized slideshow application using Web (HTML/JavaScript) clients and jWebSocket as websocket server. In an earlier article, I described the same for the Kaazing WebSocket server – https://technology.amis.nl/blog/14777/push-based-synchronized-slideshow-web-application-implemented-using-websockets-and-kaazing-websocket-gateway – and both stories are (fortunately) fairly similar. In the Kaazing article I also described a Java based Server Side ‘client’ or ‘WebSocket interaction partner’. In the jWebSocket article, that part was missing and in this article I will rectify that.

This article will describe how a Java application can integrate with jWebSocket server – in fact: start the server from within the Java code -and subsequently register listeners that intercept WebSocket communication from other interaction partners such as Web clients. The Java application can also send messages to specific clients or broadcast to all clients. This article will merely scratch the service by describing how a simple Java application interacting in this way with jWebSocket server can be created in NetBeans 7.

Finding the information to achieve this was not very easy by the way. And finding out how a stand alone Java application can connect to a stand alone (separately running) jWebSocket server is still slightly beyond my reach.

1. Create and configure the project in NetBeans

I have created a new Java Application project in NetBeans 7. I have added three jar-files as the libraries for this application.

Image

All these jar-files can be found in the JWEBSOCKET_HOME\libs directory.

Image

Note: downloading and installing jWebSocket is described in this article: https://technology.amis.nl/blog/14940/first-steps-with-jwebsocket-open-source-java-framework-for-websockets-installation-and-running-samples . This article describes configuring the environment variable JWEBSOCKET_HOME that needs to be set, referring to the directory where the jWebSocket archive was exploded and specifically to the directory that includes the bin directory. In my case: C:\temp\jWebSocketServer-1.0b1\jWebSocket-1.0.

2. Create the Java Class

The Java Class JwebSockClient that I have created will start the jWebSocket Server, then register itself as a listener on it. The code for this is straightforward:

Image

note that the jWebSocket is started based on the configuration file jwebsocket.xml that is located in JWEBSOCKET_HOME/conf:

Image

Because the jWebSocket server is started from the exact same configuration file as the stand alone server used in my earlier articles on jWebSocket – on the same host and port and with the same users and roles – the Web Client described for the Slideshow application in https://technology.amis.nl/blog/14949/push-based-synchronized-slideshow-web-application-implemented-using-websockets-and-jwebsocket-server-the-open-source-java-websocket-server is still functioning without any changes.

Image

3. Implementing the Listener methods

The JwebSockClient was registered as a listener. This can be done because the class implements WebSocketServerTokenListener. This interfaces specifies a number of methods that need to be implemented – and that form the callback handlers for jWebSocket server to invoke upon specific events.

Image

With stub implementations for all methods except processPacket:

Image

we can run the Java application like this:

Image

and verify whether the messages sent by the HTML client when new slides are selected are indeed received by the JwebSockClient:

Image

At this point, nothing useful is being done with the packets that are received, but it should be obvious that we have a way for web clients to get information to the Java server side through WebSockets.

Even more interesting of course would be the reverse route: from server to client, as that is what constitutes the challenging part of push.

4. Sending messages to (specific) connected clients

Because of the reference to the jWebSocket server instance, we can find out which clients are connected to the WebSocket ‘channels’ and we can send messages to these specific clients.

To get hold of the connected clients, we can use this code:

Image

After some Web Clients have connected, this could result in something like:

Image

We can broadcast messages – not directing them to any particular client – or send messages to specific clients. In this example, I am doing something slightly silly: I am iterating over all connectors and sending the same package to each of them. That of course amounts to broadcasting – so why not broadcast? Well, only to show the iteration over the connectors representing each of the clients and demonstration the option to send a message to a specific client.

Image

The main method of the class is slightly extended with calls to sendPacket:

Image

When this class is executed and one or several web clients run the Slideshow html page, they will all slowly progress through the slideslow, orchestrated over websocket interaction from the Java class:

Image

and a few seconds later:

Image

Note: I would still like to find out how I can connect with a standalone Java class – not one starting the jWebSocket server itself – to the jWebSocket server to start remote websocket interaction.

Resources

Download sources for this articles (NetBeans project):MyjWebSocketJavaClient.zip .

jWebSocket home page with documentation for the APIs and some how to tutorials: jwebsocket.org/

Xebia Blog article on getting started with jWebSocket: http://xebee.xebia.in/2010/10/05/web-socket-communication-using-jwebsocket/.

jWebSocket forum: http://jwebsocket.org/jws_forum/posts/list/61.page.

3 Comments

  1. Rafael April 12, 2012
    • Phoe May 12, 2012
  2. Knut Brinch January 14, 2012