java - In the program to create a simple websocket, where is the onOpen method being called? -
i found code create simple websocket connection. in main function constructor websocketclientendpoint
being called , don't see call onopen
function inside constructor. in output, "opening websocket" printed specified in onopen
function. can please tell me call onopen
function happening?
testapp.java:
package testapp; import java.net.uri; import java.net.urisyntaxexception; public class testapp { public static void main(string[] args) { try { // open websocket final websocketclientendpoint clientendpoint = new websocketclientendpoint(new uri("wss://real.okcoin.cn:10440/websocket/okcoinapi")); // add listener clientendpoint.addmessagehandler(new websocketclientendpoint.messagehandler() { public void handlemessage(string message) { system.out.println(message); } }); // send message websocket clientendpoint.sendmessage("{'event':'addchannel','channel':'ok_btccny_ticker'}"); // wait 5 seconds messages websocket thread.sleep(5000); } catch (interruptedexception ex) { system.err.println("interruptedexception exception: " + ex.getmessage()); } catch (urisyntaxexception ex) { system.err.println("urisyntaxexception exception: " + ex.getmessage()); } }
}
websocketclientendpoint.java
package testapp; import java.net.uri; import javax.websocket.clientendpoint; import javax.websocket.closereason; import javax.websocket.containerprovider; import javax.websocket.onclose; import javax.websocket.onmessage; import javax.websocket.onopen; import javax.websocket.session; import javax.websocket.websocketcontainer; /** * chatserver client * * @author jiji_sasidharan */ @clientendpoint public class websocketclientendpoint { session usersession = null; private messagehandler messagehandler; public websocketclientendpoint(uri endpointuri) { try { websocketcontainer container = containerprovider.getwebsocketcontainer(); container.connecttoserver(this, endpointuri); } catch (exception e) { throw new runtimeexception(e); } } /** * callback hook connection open events. * * @param usersession usersession opened. */ @onopen public void onopen(session usersession) { system.out.println("opening websocket"); this.usersession = usersession; } /** * callback hook connection close events. * * @param usersession usersession getting closed. * @param reason reason connection close */ @onclose public void onclose(session usersession, closereason reason) { system.out.println("closing websocket"); this.usersession = null; } /** * callback hook message events. method invoked when client send message. * * @param message text message */ @onmessage public void onmessage(string message) { if (this.messagehandler != null) { this.messagehandler.handlemessage(message); } } /** * register message handler * * @param message */ public void addmessagehandler(messagehandler msghandler) { this.messagehandler = msghandler; } /** * send message. * * @param user * @param message */ public void sendmessage(string message) { this.usersession.getasyncremote().sendtext(message); } /** * message handler. * * @author jiji_sasidharan */ public static interface messagehandler { public void handlemessage(string message); } }
you have read how annotations works understand this.the @onopen
annotation. @onopen
allows intercept creation of new session.
the annotation type onopen
@retention(value=runtime)
@target(value=method)
public @interface onopenthis method level annotation can used decorate java method wishes called when new web socket session open.
the method may take following parameters:-
optional session parameter
optional endpointconfig parameter
0 n string parameters annotated pathparam annotation.the parameters may appear in order.
Comments
Post a Comment