java - Add ServerEndpoint to Tomcat instance without annotations -
how add serverendpoint
, onopen
, onmessage
, onclose
event handlers specific class without use of @serverendpoint("/myurl")
, @onopen
, @onmessage
, @onclose
annotations within respective class, using embedded tomcat?
i believe it's along lines of:
tomcat tomcat = new tomcat(); tomcat.setport(8080); context context = tomcat.addwebapp("/", new file(webappdir).getabsolutepath()); context.setsessiontimeout(10080); servercontainer servercontainer = (servercontainer) context.getservletcontext().getattribute(servercontainer.class.getname()); serverendpointconfig serverendpointconfig = serverendpointconfig.builder.create(myclass.class, "myurl").build(); servercontainer.addendpoint(serverendpointconfig);
but servercontainer
gives java.lang.nullpointerexception
, i'm not sure if it's correct way of doing or not.
one alternative extend javax.websocket.endpoint
:
https://blogs.oracle.com/arungupta/entry/websocket_client_and_server_endpoint
public class myendpoint extends endpoint { @override public void onopen(final session session, endpointconfig ec) { session.addmessagehandler(new messagehandler.whole<string>() { @override public void onmessage(string text) { try { session.getbasicremote().sendtext(text); } catch (ioexception ex) { logger.getlogger(myendpoint.class.getname()).log(level.severe, null, ex); } } }); }
Comments
Post a Comment