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

Lucas Jellema 4
0 0
Read Time:4 Minute, 32 Second

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.

About Post Author

Lucas Jellema

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director and Oracle Developer Champion. Solution architect and developer on diverse areas including SQL, JavaScript, Kubernetes & Docker, Machine Learning, Java, SOA and microservices, events in various shapes and forms and many other things. Author of the Oracle Press book Oracle SOA Suite 12c Handbook. Frequent presenter on user groups and community events and conferences such as JavaOne, Oracle Code, CodeOne, NLJUG JFall and Oracle OpenWorld.
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

4 thoughts on “Implementing a Java Server Side component for jWebSocket Server for WebSocket interaction with Web Clients

  1. Hello, the link to the sources is broken, can you fix it? I have tried to follow the tutorial, but i only got this error:
    java.lang.NullPointerException
    at org.jwebsocket.factory.JWebSocketFactory.start(JWebSocketFactory.java:119)
    That exception is throwed in this line:

    I’m runing in a machine with linux, netbeans 7.0.1, java 1.6 and i I’ve set up the environment variable:
    JWEBSOCKET_HOME = “/home/user/programs/jWebSocket-1.0” and
    PATH=:$JWEBSOCKET_HOME/bin

    Can you help me?

    Thanks!

    1. Hello!
      Check http://enapso.org/jwsForum219/posts/list/15619.page
      You just need to import the package org.jwebsocket.config.JWebSocketConfig and add the following lines:

      // the following line must not be removed due to GNU LGPL 3.0 license!
      JWebSocketFactory.printCopyrightToConsole();
      // check if home, config or bootstrap path are passed by command line
      JWebSocketConfig.initForConsoleApp(aArgs);

  2. […] Contact About the author : Lucas Jellema Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, ADF, Java in various shapes and forms and many other things. Author of … More by Lucas Jellema « Previous post […]

  3. Hello,
    I have implemented this jWebsockt earlier but encounter some problems after the protocol update by Google.
    You seem to have a good and simple implementation, but when trying it I encounter the compile error :

    Note: C:\Test\MyjWeSocketJavaClient\src\nl\amis\jws\JwebSockClient.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    compile:
    Created dir: C:\Test\MyjWeSocketJavaClient\dist
    Copying 1 file to C:\Test\MyjWeSocketJavaClient\build
    C:\Test\MyjWeSocketJavaClient\nbproject\build-impl.xml:714: The following error occurred while executing this line:
    C:\Test\MyjWeSocketJavaClient\nbproject\build-impl.xml:541: taskdef class org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs cannot be found
    using the classloader AntClassLoader[]
    BUILD FAILED (total time: 1 second)
     
    I have not used much time trying to resolve this problem. Normally I just expect things to work.
    Anyway. It seems that you are right. Implementing a jWebsocket server is still not straightforward.

Comments are closed.

Next Post

Stand-alone Java Client for jWebSocket Server - communicating from Java Client to Web Clients and vice versa over WebSockets

In a previous article, I have discussed the open source jWebSocket Server and a simple installation and configuration (https://technology.amis.nl/blog/14940/first-steps-with-jwebsocket-open-source-java-framework-for-websockets-installation-and-running-samples). Subsequently, I have shown the implementation of a web client for a synchronized Slideshow application, where WebSocket communication is used to perform Client 2 Client push (https://technology.amis.nl/blog/14949/push-based-synchronized-slideshow-web-application-implemented-using-websockets-and-jwebsocket-server-the-open-source-java-websocket-server). In a third article […]
%d bloggers like this: