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:
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.
i dont know what version of jWebSockets are you using, i am using jWebSockets-1.0 and it works super. but anyways both the classes “org.jwebsocket.client.java.ReliabilityOptions” and “org.jwebsocket.client.token.BaseTokenClient” are located in the JAR jWebSocketJavaSEClient-1.0.jar. Following are the links:
for org.jwebsocket.client.token.BaseTokenClient
http://code.google.com/p/jwebsocket/source/browse/branches/jWebSocket-1.0/shared/J2SE/jWebSocketJavaSEClient/org/jwebsocket/client/token/BaseTokenClient.java?r=1819
check in the above code there is a constructor BaseTokenClient(ReliabilityOptions aReliabilityOptions).
for org.jwebsocket.client.java.ReliabilityOptions
http://code.google.com/p/jwebsocket/source/browse/branches/jWebSocket-1.0/shared/J2SE/jWebSocketJavaSEClient/org/jwebsocket/client/java/ReliabilityOptions.java?r=1802