Java Client for WebSocket

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!

About these ads

Howdy!

Posted in Uncategorized
3 comments on “Java Client for WebSocket
  1. [...] The (server side) Java application that also connects to the WebSocket channels can be created in any IDE. It has one dependency: on com.kaazing.gateway.client.java.core.jar, a fairly small library that supports communication with WebSockets from Java. This jar is located in the Kaazing installation, in Kaazing_Homelibclientjava. Note: this library is not Kaazing specific: it allows communication to any WebSocket server – as described by Matthias in this article: http://matthiaswessendorf.wordpress.com/2011/03/12/java-client-for-websocket/. [...]

  2. [...] The (server side) Java application that also connects to the WebSocket channels can be created in any IDE. It has one dependency: on com.kaazing.gateway.client.java.core.jar, a fairly small library that supports communication with WebSockets from Java. This jar is located in the Kaazing installation, in Kaazing_Homelibclientjava. Note: this library is not Kaazing specific: it allows communication to any WebSocket server – as described by Matthias in this article: http://matthiaswessendorf.wordpress.com/2011/03/12/java-client-for-websocket/. [...]

  3. Saulius says:

    Matthias, thanks for simple tips.

    I check the gateway-config.xml, for the “ws://localhost:8001/echo” service.
    Orginal setings:”http://localhost:8000″
    Change to:”*”

    In Google Chrome 10.0.648.204, FireFox 4.0 and IE8.0.6001.18702
    WebSocket Demo “http://localhost:8000/demo/demo.html#ws”
    works OK.

    Now Java Client for WebSocket package net.wessendorf.kaazing.gateway
    ws.connect(new URI(“ws://localhost:8001/echo”))
    in Eclipse console and runnable Java file from windows cmd console returns:
    Connection to Server is up!
    Received Event Data: Hey, server!

    Sincerely,
    Saulius

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 34 other followers

%d bloggers like this: