[jetty-users] Is static ConcurrentHashMap a reliable choice for WebSocketServlet?
Good evening, In a custom WebSocketServlet in Jetty 9.4.37.v20210219 I would like to maintain Session objects in a shared data structure. Is a static data structure like public final static Map SESSIONS = new ConcurrentHashMap<>(); a good choice for that? I have a feeling it does not work reliably. Maybe Jetty starts several Linux process and thus the static data structure is not shared among them? Because in my custom WebSocketListener I have a code: @Override public void onWebSocketText(String str) { // here the user is authenticated and mUid is found Session oldSession = SESSIONS.put(mUid, mSession); disconnect(oldSession); } private void disconnect(Session session) { LOG.info("disconnect: session={}", session); // surprisingly often session is null try { session.close(); session.disconnect(); } catch (Exception ex) { // ignore } } And often the old session printed by the above LOG is null, even though I would expect it be non-null. Best regards Alex ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
Re: [jetty-users] WebSocketListener.onWebSocketClose called numerous times
On the browser side I had automatic reconnects implemented, whenever the Websockets connection was closed. But now I am trying to detect, that a player is already connected in another browser tab and then send a custom close status 4000 from Jetty (so that automatic reconnect is disabled there and a dialog is displayed to the user, offering to reconnect): // called by the onWebSocketText Jetty-callback private void handleLogin(String username, String password) throws Exception { // check username + password and then send the player her/his open games mSession.getRemote().sendStringByFuture(mServlet.getGames(mUid)); // if oldSession is not null and still open, // then the player has several browser tabs open or // uses the mobile and the desktop app at the same time Session oldSession = SESSIONS.put(mUid, mSession); try { oldSession.close(4000, "another game session detected"); oldSession.disconnect(); } catch (Exception ex) { // ignore } However this only works sporadically, I rarely see the code 4000 in the browser. Is this caused by the half open connections too? Thank you Alex ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
Re: [jetty-users] WebSocketListener.onWebSocketClose called numerous times
Hi Joakim, I have changed my custom 9.4.37.v20210219 WebSocketServlet to @Override public void configure(WebSocketServletFactory factory) { factory.getPolicy().setIdleTimeout(5 * 60 * 1000); factory.getPolicy().setMaxBinaryMessageSize(0); factory.getPolicy().setMaxTextMessageSize(64 * 1024); factory.register(MyListener.class); factory.setCreator(new MyCreator(this))); } and have started implementing calling sendPing() every 4 minutes for the connected clients, but then I have read your other message at https://stackoverflow.com/a/54654700/165071 "Setting Max Idle Timeout and then causing the connection to not be idle by sending ping/pong isn't ideal." Do you mean by that, that if my custom WebSocketServlet will send PING every 4 minutes to the browser clients, then the connection will never be idle? Or what do you mean by "isn't ideal"? What else could be done here? Thank you Alex ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
Re: [jetty-users] Session invalidation
Difficult to tell you what is happening with so little information ;) What version of jetty? What configuration of the session management: SessionCache, SessionStore, SessionIdManager? Does your webapp involve forwarding between contexts? Does your webapp have any filters or 3rd party libraries that might create a session? Have you used a tool like Chrome's developer panel that shows you the http dialog going on between the tabs and the server? Or tcpdump/wireshark etc etc? Have you turned on DEBUG level logging for org.eclipse.jetty.server.session? How does SessionState get set onto the Session? Is it via a HttpSessionListener? If so, Request.getSession() will create a new Session if one does not exist, and the listener could set the SessionState on it. Jan On Fri, 26 Feb 2021 at 13:14, John English wrote: > I have a webapp requiring a user to log in before doing anything else. > State information is stored in an object in a session attribute called > "state". When I log out I do the following: > > HttpSession session = request.getSession(); > if (session != null) { >session.removeAttribute("state"); >try { > session.invalidate(); >} >catch (IllegalStateException e) { } > } > > I go to the webapp and log in, then open another tab for the same webapp > and log out, executing the code above. I then go back to the first tab > and click a button which sends a POST request to a servlet that starts > off like this: > > HttpSession session = request.getSession(); > SessionState state = (session != null ? > (SessionState)session.getAttribute("state") : null); > > The session and the state are both valid objects after these two lines. > > Can anyone tell me what might be happening here? > > -- > John English > ___ > jetty-users mailing list > jetty-users@eclipse.org > To unsubscribe from this list, visit > https://www.eclipse.org/mailman/listinfo/jetty-users > -- Jan Bartel www.webtide.com *Expert assistance from the creators of Jetty and CometD* ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
Re: [jetty-users] WebSocketListener.onWebSocketClose called numerous times
Thank you, Joakim! ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users