Java Code Examples for javax.websocket.Session#getOpenSessions()
The following examples show how to use
javax.websocket.Session#getOpenSessions() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RoomEndpoint.java From sample-room-java with Apache License 2.0 | 6 votes |
/** * Simple broadcast: loop over all mentioned sessions to send the message * <p> * We are effectively always broadcasting: a player could be connected * to more than one device, and that could correspond to more than one connected * session. Allow topic filtering on the receiving side (Mediator and browser) * to filter out and display messages. * * @param session Target session (used to find all related sessions) * @param message Message to send * @see #sendRemoteTextMessage(Session, Message) */ @Timed(name = "websocket_sendMessage_timer", reusable = true, tags = "label=websocket") @Counted(name = "websocket_sendMessage_count", monotonic = true, reusable = true, tags = "label=websocket") @Metered(name = "websocket_sendMessage_meter", reusable = true, tags = "label=websocket") public void sendMessage(Session session, Message message) { for (Session s : session.getOpenSessions()) { sendMessageToSession(s, message); } }
Example 2
Source File: ChatEndpoint.java From training with MIT License | 5 votes |
@OnMessage public void onMessage(final Session session, final ChatMessage chatMessage) { String room = (String) session.getUserProperties().get("room"); try { for (Session s : session.getOpenSessions()) { if (s.isOpen() && room.equals(s.getUserProperties().get("room"))) { s.getBasicRemote().sendObject(chatMessage); } } } catch (IOException | EncodeException e) { log.log(Level.WARNING, "onMessage failed", e); } }
Example 3
Source File: ChatEndpoint.java From training with MIT License | 5 votes |
@OnMessage public void onMessage(final Session session, final ChatMessage chatMessage) { String room = (String) session.getUserProperties().get("room"); try { for (Session s : session.getOpenSessions()) { if (s.isOpen() && room.equals(s.getUserProperties().get("room"))) { s.getBasicRemote().sendObject(chatMessage); } } } catch (IOException | EncodeException e) { log.log(Level.WARNING, "onMessage failed", e); } }
Example 4
Source File: TestWsWebSocketContainer.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Test public void testGetOpenSessions() throws Exception { Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); ctx.addApplicationListener(TesterEchoServer.Config.class.getName()); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMappingDecoded("/", "default"); tomcat.start(); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); EndpointA endpointA = new EndpointA(); Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); EndpointB endpointB = new EndpointB(); Session s1b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC); Session s2b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC); Set<Session> setA = s3a.getOpenSessions(); Assert.assertEquals(3, setA.size()); Assert.assertTrue(setA.remove(s1a)); Assert.assertTrue(setA.remove(s2a)); Assert.assertTrue(setA.remove(s3a)); s1a.close(); setA = s3a.getOpenSessions(); Assert.assertEquals(2, setA.size()); Assert.assertFalse(setA.remove(s1a)); Assert.assertTrue(setA.remove(s2a)); Assert.assertTrue(setA.remove(s3a)); Set<Session> setB = s1b.getOpenSessions(); Assert.assertEquals(2, setB.size()); Assert.assertTrue(setB.remove(s1b)); Assert.assertTrue(setB.remove(s2b)); // Close sessions explicitly as Gump reports a session remains open at // the end of this test s2a.close(); s3a.close(); s1b.close(); s2b.close(); }
Example 5
Source File: TestWsWebSocketContainer.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Test public void testSessionExpirySession() throws Exception { Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); ctx.addApplicationListener(TesterEchoServer.Config.class.getName()); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMappingDecoded("/", "default"); tomcat.start(); // Need access to implementation methods for configuring unit tests WsWebSocketContainer wsContainer = (WsWebSocketContainer) ContainerProvider.getWebSocketContainer(); // 5 second timeout wsContainer.setDefaultMaxSessionIdleTimeout(5000); wsContainer.setProcessPeriod(1); EndpointA endpointA = new EndpointA(); Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); s1a.setMaxIdleTimeout(3000); Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); s2a.setMaxIdleTimeout(6000); Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); s3a.setMaxIdleTimeout(9000); // Check all three sessions are open Set<Session> setA = s3a.getOpenSessions(); int expected = 3; while (expected > 0) { Assert.assertEquals(expected, getOpenCount(setA)); int count = 0; while (getOpenCount(setA) == expected && count < 50) { count ++; Thread.sleep(100); } expected--; } Assert.assertEquals(0, getOpenCount(setA)); }
Example 6
Source File: TestWsWebSocketContainer.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
@Test public void testGetOpenSessions() throws Exception { Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); ctx.addApplicationListener(TesterEchoServer.Config.class.getName()); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); tomcat.start(); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); EndpointA endpointA = new EndpointA(); Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); EndpointB endpointB = new EndpointB(); Session s1b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC); Session s2b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC); Set<Session> setA = s3a.getOpenSessions(); Assert.assertEquals(3, setA.size()); Assert.assertTrue(setA.remove(s1a)); Assert.assertTrue(setA.remove(s2a)); Assert.assertTrue(setA.remove(s3a)); s1a.close(); setA = s3a.getOpenSessions(); Assert.assertEquals(2, setA.size()); Assert.assertFalse(setA.remove(s1a)); Assert.assertTrue(setA.remove(s2a)); Assert.assertTrue(setA.remove(s3a)); Set<Session> setB = s1b.getOpenSessions(); Assert.assertEquals(2, setB.size()); Assert.assertTrue(setB.remove(s1b)); Assert.assertTrue(setB.remove(s2b)); }
Example 7
Source File: TestWsWebSocketContainer.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
@Test public void testSessionExpirySession() throws Exception { Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); ctx.addApplicationListener(TesterEchoServer.Config.class.getName()); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); tomcat.start(); // Need access to implementation methods for configuring unit tests WsWebSocketContainer wsContainer = (WsWebSocketContainer) ContainerProvider.getWebSocketContainer(); // 5 second timeout wsContainer.setDefaultMaxSessionIdleTimeout(5000); wsContainer.setProcessPeriod(1); EndpointA endpointA = new EndpointA(); Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); s1a.setMaxIdleTimeout(3000); Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); s2a.setMaxIdleTimeout(6000); Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); s3a.setMaxIdleTimeout(9000); // Check all three sessions are open Set<Session> setA = s3a.getOpenSessions(); int expected = 3; while (expected > 0) { Assert.assertEquals(expected, getOpenCount(setA)); int count = 0; while (getOpenCount(setA) == expected && count < 5) { count ++; Thread.sleep(1000); } expected--; } Assert.assertEquals(0, getOpenCount(setA)); }
Example 8
Source File: TestWsWebSocketContainer.java From tomcatsrc with Apache License 2.0 | 4 votes |
@Test public void testGetOpenSessions() throws Exception { Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); ctx.addApplicationListener(TesterEchoServer.Config.class.getName()); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); tomcat.start(); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); EndpointA endpointA = new EndpointA(); Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); EndpointB endpointB = new EndpointB(); Session s1b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC); Session s2b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC); Set<Session> setA = s3a.getOpenSessions(); Assert.assertEquals(3, setA.size()); Assert.assertTrue(setA.remove(s1a)); Assert.assertTrue(setA.remove(s2a)); Assert.assertTrue(setA.remove(s3a)); s1a.close(); setA = s3a.getOpenSessions(); Assert.assertEquals(2, setA.size()); Assert.assertFalse(setA.remove(s1a)); Assert.assertTrue(setA.remove(s2a)); Assert.assertTrue(setA.remove(s3a)); Set<Session> setB = s1b.getOpenSessions(); Assert.assertEquals(2, setB.size()); Assert.assertTrue(setB.remove(s1b)); Assert.assertTrue(setB.remove(s2b)); // Close sessions explicitly as Gump reports a session remains open at // the end of this test s2a.close(); s3a.close(); s1b.close(); s2b.close(); }
Example 9
Source File: TestWsWebSocketContainer.java From tomcatsrc with Apache License 2.0 | 4 votes |
@Test public void testSessionExpirySession() throws Exception { Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); ctx.addApplicationListener(TesterEchoServer.Config.class.getName()); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); tomcat.start(); // Need access to implementation methods for configuring unit tests WsWebSocketContainer wsContainer = (WsWebSocketContainer) ContainerProvider.getWebSocketContainer(); // 5 second timeout wsContainer.setDefaultMaxSessionIdleTimeout(5000); wsContainer.setProcessPeriod(1); EndpointA endpointA = new EndpointA(); Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); s1a.setMaxIdleTimeout(3000); Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); s2a.setMaxIdleTimeout(6000); Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC); s3a.setMaxIdleTimeout(9000); // Check all three sessions are open Set<Session> setA = s3a.getOpenSessions(); int expected = 3; while (expected > 0) { Assert.assertEquals(expected, getOpenCount(setA)); int count = 0; while (getOpenCount(setA) == expected && count < 50) { count ++; Thread.sleep(100); } expected--; } Assert.assertEquals(0, getOpenCount(setA)); }