package org.netevolved.marine; import java.net.URI; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.ijsolutions.testbench.livetest.LiveTestManager; import org.json.simple.parser.JSONParser; import com.neovisionaries.ws.client.ThreadType; import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocketException; import com.neovisionaries.ws.client.WebSocketFactory; import com.neovisionaries.ws.client.WebSocketFrame; import com.neovisionaries.ws.client.WebSocketListener; import com.neovisionaries.ws.client.WebSocketState; public class SignalKClient implements WebSocketListener { private static final Log log = LogFactory.getLog(LiveTestManager.class); protected ThreadLocal perThreadCache = new ThreadLocal(); protected boolean disconnect = false; protected boolean autoreconnect = false; WebSocket socket; protected String fieldUri; public String getUri() { return fieldUri; } public void setUri(String inUri) { fieldUri = inUri; } public boolean connect() { WebSocketFactory factory = new WebSocketFactory() ;// .setConnectionTimeout(TIMEOUT); try { URI uri = new URI(getUri()); socket = factory.createSocket(uri); socket.addListener(this); socket.connect(); socket.setPingInterval(30000); //Every 30 seconds return true; } catch(Throwable e) { return false; } } public JSONParser getJSONParser() { JSONParser jSONParser = (JSONParser)perThreadCache.get(); if (jSONParser == null) { jSONParser = new JSONParser(); perThreadCache.set(jSONParser); } return jSONParser; } private void send(String inText) { socket.sendText(inText); } @Override public void onTextMessage(WebSocket inWebsocket, String inMessage) throws Exception { //log.info(inMessage); } public void disconnect() { // TODO Auto-generated method stub disconnect = true; socket.disconnect(); } public boolean isConnected() { return socket.isOpen(); } public boolean reconnect() { try { socket.connect(); } catch (WebSocketException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } // public boolean connect(String server, String inUname, String inKey) // { // try // { //// if( getModel().getConnection() != null && getModel().getConnection().isClosing() ) //// { //// //return false; //// getModel().getConnection().disconnect(); //// getModel().setConnection(null); //// } // int i; // if ((i = server.indexOf("/", 8)) > -1) // { // //strip off /s // server = server.substring(0, i); // } // String prefix = server.substring(server.lastIndexOf("/") + 1); // if( server.startsWith("https")) // { // prefix = "wss://" + prefix; // } // else // { // prefix = "ws://" + prefix; // } // String url = prefix + "/entermedia/services/websocket/org/entermediadb/websocket/mediaboat/MediaBoatConnection"; // url = url + "?userid=" + inUname; // log.info("Connecting to:" + url); // URI uri = new URI(url); // //// WsConnection connection = new WsConnection(uri); //// connection.setAppController(this); //// if( !connection.connect() ) //// { //// debug("Could not connect"); //// return false; //// } //// debug("Connected ok"); //// getModel().setConnection(connection); //// // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts // //return getModel().login(uri, server,inUname,inKey); // } // catch( Exception ex) // { // //reportError("Could not connect" , ex); // ex.printStackTrace(); // } // //Send client info // return false; // } @Override public void onStateChanged(WebSocket inWebsocket, WebSocketState inNewState) throws Exception { // TODO Auto-generated method stub } @Override public void onConnected(WebSocket inWebsocket, Map> inHeaders) throws Exception { // TODO Auto-generated method stub } @Override public void onConnectError(WebSocket inWebsocket, WebSocketException inCause) throws Exception { // TODO Auto-generated method stub } @Override public void onDisconnected(WebSocket inWebsocket, WebSocketFrame inServerCloseFrame, WebSocketFrame inClientCloseFrame, boolean inClosedByServer) throws Exception { if( !disconnect ) { reconnect(); } } @Override public void onFrame(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onContinuationFrame(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onTextFrame(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onBinaryFrame(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onCloseFrame(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onPingFrame(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub //getAppController().info("Ping received"); } @Override public void onPongFrame(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub // log.info("Pong recieved"); // getAppController().info("Pong received"); } @Override public void onBinaryMessage(WebSocket inWebsocket, byte[] inBinary) throws Exception { // TODO Auto-generated method stub } @Override public void onSendingFrame(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onFrameSent(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onFrameUnsent(WebSocket inWebsocket, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onThreadCreated(WebSocket inWebsocket, ThreadType inThreadType, Thread inThread) throws Exception { // TODO Auto-generated method stub } @Override public void onThreadStarted(WebSocket inWebsocket, ThreadType inThreadType, Thread inThread) throws Exception { // TODO Auto-generated method stub } @Override public void onThreadStopping(WebSocket inWebsocket, ThreadType inThreadType, Thread inThread) throws Exception { // TODO Auto-generated method stub } @Override public void onError(WebSocket inWebsocket, WebSocketException inCause) throws Exception { // TODO Auto-generated method stub } @Override public void onFrameError(WebSocket inWebsocket, WebSocketException inCause, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onMessageError(WebSocket inWebsocket, WebSocketException inCause, List inFrames) throws Exception { // TODO Auto-generated method stub } @Override public void onMessageDecompressionError(WebSocket inWebsocket, WebSocketException inCause, byte[] inCompressed) throws Exception { // TODO Auto-generated method stub } @Override public void onTextMessageError(WebSocket inWebsocket, WebSocketException inCause, byte[] inData) throws Exception { // TODO Auto-generated method stub } @Override public void onSendError(WebSocket inWebsocket, WebSocketException inCause, WebSocketFrame inFrame) throws Exception { // TODO Auto-generated method stub } @Override public void onUnexpectedError(WebSocket inWebsocket, WebSocketException inCause) throws Exception { // TODO Auto-generated method stub } @Override public void handleCallbackError(WebSocket inWebsocket, Throwable inCause) throws Exception { // TODO Auto-generated method stub } @Override public void onSendingHandshake(WebSocket inWebsocket, String inRequestLine, List inHeaders) throws Exception { // TODO Auto-generated method stub } @Override public void onTextMessage(WebSocket inArg0, byte[] inArg1) throws Exception { log.info(new String(inArg1)); } }