java - WebSocket: OnClose() is never called -
i'm implementing application websocket endpoint. here code:
@applicationscoped @serverendpoint(value="/socket", encoders = {messageencoder.class, commandencoder.class}) public class socketendpoint { /** default-logger */ private final static logger log = loggerfactory.getlogger(socketendpoint.class); @inject sessionhandler sessionhandler; @onopen public void open(session session, endpointconfig config) { log.debug("connected session => '{}' - '{}'", session, config); sessionhandler.initsession(session); } @onmessage public void onmessage(session session, string messagejson) { // } @onclose public void onclose(session session, closereason reason) { log.debug("closing session => '{}' - '{}'", session, reason); sessionhandler.removesession(session); } @onerror public void onerror(session session, throwable ex) { log.error("websocket error => '{}'", ex.getmessage()); } }
one of encode class looks this:
public class messageencoder implements encoder.text<message> { /** default-logger */ private final static logger log = loggerfactory.getlogger(messageencoder.class); @override public void init(endpointconfig config) { log.debug("init messageencoder"); } @override public void destroy() { log.debug("destroy messageencoder"); } @override public string encode(messagee message) throws encodeexception { return message.tostring(); } }
opening websocket calls socketendpoint.open()
expected. closing websocket calls messageencoder.destroy()
not socketendpoint.close()
.
can give me advise, did wrong? without solution have check manually if registered sessions still alive since messageencoder.destroy()
has no parameters.
thanks in advance!
update
just implemented dummy endpoint:
@applicationscoped @serverendpoint("/dummy") public class dummyendpoint { /** default-logger */ private final static logger log = loggerfactory.getlogger(dummyendpoint.class); @onopen public void open(session session, endpointconfig config) { log.debug("connected session principal => '{}'", session.getid()); } @onmessage public void onmessage(session session, string messagejson) { log.debug("on message => '{}' => '{}'", session.getid(), messagejson); } @onclose public void onclose(session session, closereason reason) { log.debug("closing session => '{}' - '{}'", session, reason); } @onerror public void onerror(session session, throwable ex) { log.error("websocket error => '{}' => '{}'", session, ex.getmessage()); } }
when using dummy endpoint @onclose
called. can see 1 major difference socketendpoint
class: dummyendpoint
not use encoder
classes.
any hints?
as mentioned in comments, code working fine. if start wildfly-websocket-quickstart, adding @onclose
decorated method on serverendpoint
, works fine using wildfly 10.x, , recent browsers (for instance chrome v59.x). example of serverendpoint working here (to use @inject
don't forget add beans.xml
in web-inf folder):
@applicationscoped @serverendpoint(value="/shout", encoders = {messageencoder.class}) public class shoutserverendpoint { @inject sessionhandler s; @onopen public void open(session session, endpointconfig config) throws exception { s.initsession(session); } @onmessage public void shout(string text, session client) { system.out.println("session: " + client + " has text: " + text); message m = new message(); try { client.getbasicremote().sendobject(m);//use encoder write dummy message } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (encodeexception e) { // todo auto-generated catch block e.printstacktrace(); } client.getasyncremote().sendtext(text.touppercase()); } @onclose public void onclose(session client, closereason reason){ system.out.println("session " + client + " closing " + reason); s.destroysession(client); } @onerror public void onerror(session session, throwable ex) { system.out.println("error: " + ex.getmessage() ); } }
thus culprit seems there older version of code used wildfly not cleaned during redeployment of webapp, instance, using eclipse, worth in case of strange behavior use clean
option on server used (see: this eclipse doc)
if deploying directly wildfly, can clean resources deleting in (from this article):
/[wildfly-location]/standalone/data
/[wildfly-location]/standalone/deployments
/[wildfly-location]/standalone/tmp
it ensures no older copies of code remains during future deployment.
Comments
Post a Comment