Java Client for WebSocket - No Fluff Just Stuff

Java Client for WebSocket

Posted by: Matthias Wessendorf on March 11, 2011

When folks hear WebSocket, they most-likely imaging using the (native) JavaScript APIs, from modern browsers, to access a WebSocket server. However there is a need for other platforms, like a regular Java client. Several Http Client projects, like Async Http Client or Apache’s HttpClient , have already plans to add WebSocket support.

The Kaazing WebSocket Gateway is an advanced WebSocket server, which contains several Client SDKs, including a Java-based WebSocket client!

After downloading and extracting the Kaazing WebSocket Gateway, you find the Java API inside the lib/client/java folder. Add the JAR to a (blank) Java Project in your IDE (e.g. Eclipse).

A pretty quick (and simple) test is using the API against the public Echo-Server, hosted at websocket.org,like:


package net.wessendorf.kaazing.gateway;

import java.net.URI;
import com.kaazing.gateway.client.html5.WebSocket;
import com.kaazing.gateway.client.html5.WebSocketAdapter;
import com.kaazing.gateway.client.html5.WebSocketEvent;

public class WebSocketTest {

  public static void main(String[] args) throws Exception {
    final WebSocket ws = new WebSocket();
    ws.addWebSocketListener(
          new WebSocketAdapter() {
     @Override
     public void onMessage(WebSocketEvent messageEvent) {
         System.out.println("Received Event Data: " + messageEvent.getData());
         // let's close the open connection...
         try {
             ws.close();
         }
         catch (Exception e) {
             e.printStackTrace();
         }
     }
     @Override
     public void onOpen(WebSocketEvent openEvent) {
         System.out.println("Connection to Server is up!");
          // we are able to talk to the WebSocket gateway
         try {
             ws.send("Hey, server!");
         }
         catch (Exception e) {
             e.printStackTrace();
         }
            }
          }
        );
        ws.connect(new URI("ws://echo.websocket.org:80"));
    }
}

The Java code is pretty straightforward: It creates a WebSocket object, overrides a convenience class to react on some WebSocket events and finally connects to the actual Echo-Server.

Once the connection is up (inside the onOpen() callback), we are sending a simple text to the server. The server returns exactly the same string back to the sending client. This issues our onMessage() callback. After printing the received data to the console, we close the open connection.

Instead of using the remote server, you could try the demos on your machine, by installing the Kaazing WebSocket Gateway on your box!

Have fun!


Matthias  Wessendorf

About Matthias Wessendorf

Matthias Wessendorf is a software developer at Oracle. He currently works on ADF Faces, which is an Ajax-based JSF component suite. Matthias also contributes to the OpenSource community, mainly Apache MyFaces and Apache Trinidad. Before joining Oracle, he worked as a CMS-Developer at pironet, where he was building a next-generation CMS, using UI technologies like XUL and Ajax.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »