[WebSocket] Java client for jWebSocket Server


jWebSocket is an open source Java and JavaScript implementation of the HTML5 WebSocket protocol with a huge set of extensions. Check out http://jwebsocket.org/ for more information.

Following program is an extract from a Java Demo(jWebSocketAdmin) application included with the jWebSocket Server zip archive. The test program below does 3 simple things:

1. connects to jWebSocket Server

2. Broadcasts Message

3. closes the connection

Program requires 3 JARS: jWebSocketClientAPI-1.0.jar, jWebSocketCommon-1.0.jar and jWebSocketJavaSEClient-1.0.jar which can be found in the libs directory in the Server zip download.

package test;

import org.jwebsocket.client.java.ReliabilityOptions;
import org.jwebsocket.client.token.BaseTokenClient;
import org.jwebsocket.kit.WebSocketException;

/**
*
* @author dom009
*/
public class test {
private BaseTokenClient mClient = null;
private ReliabilityOptions mReliabilityOptions = null;

public test(){
try {
mReliabilityOptions = new ReliabilityOptions(true, 1500, 3000, 1, -1); // client settings
mClient = new BaseTokenClient(mReliabilityOptions);
mClient.open(13, “ws://localhost:8787/jWebSocket/jWebSocket”); //connects
mClient.broadcastText(“Test message”); //broadcast
mClient.close(); //closes connection
} catch (WebSocketException ex) {
System.out.println(“Exception : ” + ex);
}
}
public static void main(String[] args) {
new test();
}
}

There is also a very simple java client – Weberknecht (http://code.google.com/p/weberknecht/ nice simple code on the project home page) available for jWebSocket Server but it just sends messages and does not broadcasts them.

NOTE: the test program above is a Java Client and expects a jWebSocket 1.0 server running at port 8787 on the local machine (ws://localhost:8787/jWebSocket/jWebSocket”).

UPDATE: the above test code works fine as a standalone program, but for the same code to work on a web application there is another JAR file required javolution-5.5.1.jar, which is also located in the Server zip dowload.

Further Reading:

http://en.wikipedia.org/wiki/WebSocket

http://jwebsocket.org/

Advertisements

About Dominic

J for JAVA more about me : http://about.me/dominicdsouza
This entry was posted in Thechy Stuff and tagged , , , , , . Bookmark the permalink.

2 Responses to [WebSocket] Java client for jWebSocket Server

  1. jouk says:

    it didn’t work. I downloaded the jar jwebsocketSEclient jar. but I can’t find ReliabilityOptions class in there. Even if I put the source that I found on the internet, I faced another error. BaseTokenClient class didn’t provide that kind of constructor.

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