Java Code Examples for javax.websocket.Session#setMaxIdleTimeout()

The following examples show how to use javax.websocket.Session#setMaxIdleTimeout() . 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: WsConfigurator.java    From mercury with Apache License 2.0 6 votes vote down vote up
public void update(Session session) {
    session.setMaxIdleTimeout(getIdleTimeout() * 1000);
    if (!idleTimerLogged) {
        idleTimerLogged = true;
        log.info("{} = {} seconds", IDLE_TIMEOUT, session.getMaxIdleTimeout() / 1000);
    }
    // adjust web socket buffer size
    int originalTextSize = session.getMaxTextMessageBufferSize();
    int originalBinarySize = session.getMaxBinaryMessageBufferSize();
    if (originalTextSize != getTextSize()) {
        session.setMaxTextMessageBufferSize(getTextSize());
        if (!textSizeLogged) {
            textSizeLogged = true;
            log.warn("{} changed from {} to {}", TEXT_SIZE, originalTextSize, session.getMaxTextMessageBufferSize());
        }
    }
    if (originalBinarySize != getBinarySize()) {
        session.setMaxBinaryMessageBufferSize(getBinarySize());
        if (!binarySizeLogged) {
            binarySizeLogged = true;
            log.warn("{} changed from {} to {}", BINARY_SIZE, originalBinarySize, session.getMaxBinaryMessageBufferSize());
        }
    }
}
 
Example 2
Source File: AnnotatedEndpointTest.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("UT3 - P4")
public void testClientSideIdleTimeout() throws Exception {
    //make a sub class
    CountDownLatch latch = new CountDownLatch(1);
    CloseCountdownEndpoint c = new CloseCountdownEndpoint(latch);

    Session session = deployment.connectToServer(c, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/chat/Bob"));
    session.setMaxIdleTimeout(100);
    Assert.assertTrue(latch.await(2000, TimeUnit.MILLISECONDS));
    Assert.assertFalse(session.isOpen());

}
 
Example 3
Source File: WebSocketClientEndpoint.java    From rogue-cloud with Apache License 2.0 5 votes vote down vote up
@OnOpen
public void open(Session session) {
	System.out.println("open.");
	
	session.setMaxIdleTimeout(2 * TimeUnit.MILLISECONDS.convert(RCSharedConstants.MAX_ROUND_LENGTH_IN_NANOS, TimeUnit.NANOSECONDS));
	
	// Convert the session to a 'managed resource', so that it will automatically be disposed of once it expires.
	ResourceLifecycleUtil.getInstance().addNewSession(ServerWsClientUtil.convertSessionToManagedResource(session));
}
 
Example 4
Source File: GameRoundWebsocket.java    From liberty-bikes with Eclipse Public License 1.0 5 votes vote down vote up
@OnOpen
public void onOpen(@PathParam("roundId") String roundId, Session session) {
    log(roundId, "Opened a session");
    session.setMaxTextMessageBufferSize(1000);
    session.setMaxBinaryMessageBufferSize(1000);
    session.setMaxIdleTimeout(90 * 1000);
    timerContext = GameMetrics.timerStart(GameMetrics.openWebsocketTimerMetadata);
}
 
Example 5
Source File: BasicWebSocketEndpoint.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@OnOpen
public void onOpen(Session session) {
  String combinedEndpointId = getOrGenerateCombinedEndpointId(session);

  LOG.debug("Web socket session opened");
  LOG.debug("Endpoint: {}", combinedEndpointId);

  session.setMaxIdleTimeout(0);

  registry.add(combinedEndpointId, session);
  reSender.resend(combinedEndpointId);
  sessionMessagesBuffer.put(session, new StringBuffer());
}
 
Example 6
Source File: TestWsWebSocketContainer.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@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 7
Source File: TimeoutEndpoint.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@OnOpen
public void open(Session session) {
    session.setMaxIdleTimeout(100);
}
 
Example 8
Source File: TestWsWebSocketContainer.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@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 9
Source File: WebSocketBrowserEndpoint.java    From rogue-cloud with Apache License 2.0 4 votes vote down vote up
@OnOpen
public void open(Session session) {
	System.out.println("open.");
	session.setMaxIdleTimeout(2 * TimeUnit.MILLISECONDS.convert(RCSharedConstants.MAX_ROUND_LENGTH_IN_NANOS, TimeUnit.NANOSECONDS));
	ResourceLifecycleUtil.getInstance().addNewSession(ServerWsClientUtil.convertSessionToManagedResource(session));
}
 
Example 10
Source File: TestWsWebSocketContainer.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@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));
}
 
Example 11
Source File: TerminalService.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
public void onWebSocketConnect(Session webSocketSession) {
  this.webSocketSession = webSocketSession;
  webSocketSession.setMaxIdleTimeout(60 * 60 * 1000);
}