Hi,
i try to build a Java Client Websocket implemenation.
I downloaded the sl4f.jars from
http://www.slf4j.org/download.html and
included the
- slf4j-api-1.6.4.jar
- slf4j-simple-1.6.4.jar
in my eclipse project to build path.
I also downloaded the apache commons codec 1.6 from
http://commons.apache.org/codec/download_codec.cgi and included
- commons-codex-1.6.jar
in my eclipse project to build path.
Also Netty from
http://www.jboss.org/netty/downloads.html and included
netty-3.2.7.Final.jar and the websocket-0.9.2.1.jar from
http://code.google.com/p/unitt/downloads/list .
My Approach look like this:
MyWebSocket.java
package de.test;
import java.net.URI;
import java.util.List;
import com.unitt.framework.websocket.WebSocket;
import com.unitt.framework.websocket.WebSocketConnectConfig;
import
com.unitt.framework.websocket.WebSocketConnectConfig.WebSocketVersion;
import com.unitt.framework.websocket.WebSocketObserver;
import com.unitt.framework.websocket.netty.ClientWebsocketFactory;
import com.unitt.framework.websocket.simple.SimpleSocketFactory;
public class MyWebSocket implements WebSocketObserver {
private WebSocket ws;
@Override
public void onBinaryMessage(byte[] arg0) {
// TODO Auto-generated method stub
System.out.println("onBinaryMessage");
}
@Override
public void onClose(int arg0, String arg1, Exception arg2) {
// TODO Auto-generated method stub
System.out.println("onClose");
}
@Override
public void onError(Exception arg0) {
// TODO Auto-generated method stub
System.out.println("onError");
}
@Override
public void onOpen(String arg0, List<String> arg1) {
// TODO Auto-generated method stub
System.out.println("onOpen");
}
@Override
public void onPong(String arg0) {
// TODO Auto-generated method stub
System.out.println("onPong");
}
@Override
public void onTextMessage(String arg0) {
// TODO Auto-generated method stub
System.out.println("onTextMessage: " + arg0.toString());
}
public MyWebSocket(URI url){
WebSocketConnectConfig config = new WebSocketConnectConfig();
config.setUrl(url);
config.setWebSocketVersion(WebSocketVersion.Version08); // 10 is not
supproted by jetty
ws = SimpleSocketFactory.create(config, this);
}
public void openMysocket(){
ws.open();
}
public void sendMsg(String msg){
ws.sendMessage(msg);
}
public void closeMysocket(){
ws.close();
}
}
startClient.java:
package de.test;
import java.net.URI;
import java.net.URISyntaxException;
public class startClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
URI serverurl = new URI("ws://
172.30.17.110:8080/WSTest/
WebServletTest");
MyWebSocket sock = new MyWebSocket(serverurl);
sock.openMysocket();
sock.sendMsg("hello world");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sock.closeMysocket();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I tested with Jetty and the console in eclipse shows the following:
onOpen
Closing with (0): null
Message Bytes:
Fragment:
Bytes: 88,80,f5,f4,a,2e,
onClose
I did not wrote an extra server implementation, but used the jetty
build-in implementation for websockets. I tested the jetty websocket
servlet with another websocket client using weberknecht-websockets.
Have i to use an extra implementation possibly provided by unitt-
websockets?
thank you for your help in advance
best regards
hsch