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.
All these jar-files can be found in the JWEBSOCKET_HOME\libs directory.
Note: downloading and installing jWebSocket is described in this article: http://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:
note that the jWebSocket is started based on the configuration file jwebsocket.xml that is located in JWEBSOCKET_HOME/conf:
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 http://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.
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.
With stub implementations for all methods except processPacket:
we can run the Java application like this:
and verify whether the messages sent by the HTML client when new slides are selected are indeed received by the JwebSockClient:
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:
After some Web Clients have connected, this could result in something like:
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.
The main method of the class is slightly extended with calls to sendPacket:
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:
and a few seconds later:
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.
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.