File tree Expand file tree Collapse file tree 1 file changed +10
-1
lines changed
Expand file tree Collapse file tree 1 file changed +10
-1
lines changed Original file line number Diff line number Diff line change 66import org .java_websocket .drafts .Draft_10 ;
77import org .java_websocket .handshake .ServerHandshake ;
88
9+ /** This example demonstrates how to create a websocket connection to a server. Only the most important callbacks are overloaded. */
910public class ExampleClient extends WebSocketClient {
1011
1112 public ExampleClient ( URI serverUri , Draft draft ) {
@@ -18,22 +19,30 @@ public ExampleClient( URI serverURI ) {
1819
1920 @ Override
2021 public void onOpen ( ServerHandshake handshakedata ) {
22+ System .out .println ( "opened connection" );
23+ // if you pan to refuse connection based on ip or httpfields overload: onWebsocketHandshakeReceivedAsClient
2124 }
2225
2326 @ Override
2427 public void onMessage ( String message ) {
28+ System .out .println ( "received: " + message );
29+ // send( "you said: " + message );
2530 }
2631
2732 @ Override
2833 public void onClose ( int code , String reason , boolean remote ) {
34+ // The codecodes are documented in class org.java_websocket.framing.CloseFrame
35+ System .out .println ( "Connection closed by " + ( remote ? "remote peer" : "us" ) );
2936 }
3037
3138 @ Override
3239 public void onError ( Exception ex ) {
40+ ex .printStackTrace ();
41+ // if the error is fatal then onClose will be called additionally
3342 }
3443
3544 public static void main ( String [] args ) throws URISyntaxException {
36- ExampleClient c = new ExampleClient ( new URI ( "ws://localhost:8887" ), new Draft_10 () );
45+ ExampleClient c = new ExampleClient ( new URI ( "ws://localhost:8887" ), new Draft_10 () ); // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts
3746 c .connect ();
3847 }
3948
You can’t perform that action at this time.
0 commit comments