package server; /** * Created by effi on 4/28/15. */ import org.java_websocket.WebSocketImpl; import org.java_websocket.client.WebSocketClient; import org.java_websocket.drafts.Draft_10; import org.java_websocket.framing.Framedata; import org.java_websocket.handshake.ServerHandshake; import org.java_websocket.drafts.Draft; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; /** * Created by effi on 4/26/15. * Client for Webserver Connection * */ public class TestClient extends WebSocketClient { public TestClient( URI serverUri , Draft draft ) { super( serverUri, draft ); } public TestClient( URI serverURI ) { super( serverURI ); } @Override public void onOpen( ServerHandshake handshakedata ) { System.out.println( "opened connection" ); // if you plan to refuse connection based on ip or httpfields overload: onWebsocketHandshakeReceivedAsClient } @Override public void onMessage( String message ) { System.out.println( "received: " + message ); } public void onFragment( Framedata fragment ) { System.out.println( "received fragment: " + new String( fragment.getPayloadData().array() ) ); } @Override public void onClose( int code, String reason, boolean remote ) { // The codecodes are documented in class org.java_websocket.framing.CloseFrame System.out.println("Connection closed by " + (remote ? "remote peer" : "us")); } @Override public void onError( Exception ex ) { ex.printStackTrace(); // if the error is fatal then onClose will be called additionally } public static void main(String[] args) { TestClient c = null; // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts try { c = new TestClient( new URI( "ws://phil-m.eu:8887" ), new Draft_10() ); } catch (URISyntaxException e) { e.printStackTrace(); } c.connect(); //sleep after connection try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } c.send("test"); } }