Search in sources :

Example 16 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession in project spring-framework by spring-projects.

the class WebSocketServerSockJsSessionTests method afterSessionInitializedOpenFrameFirst.

@Test
@SuppressWarnings("resource")
public void afterSessionInitializedOpenFrameFirst() throws Exception {
    TextWebSocketHandler handler = new TextWebSocketHandler() {

        @Override
        public void afterConnectionEstablished(WebSocketSession session) throws Exception {
            session.sendMessage(new TextMessage("go go"));
        }
    };
    TestWebSocketServerSockJsSession session = new TestWebSocketServerSockJsSession(this.sockJsConfig, handler, null);
    session.initializeDelegateSession(this.webSocketSession);
    List<TextMessage> expected = Arrays.asList(new TextMessage("o"), new TextMessage("a[\"go go\"]"));
    assertEquals(expected, this.webSocketSession.getSentMessages());
}
Also used : TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) TestWebSocketServerSockJsSession(org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSessionTests.TestWebSocketServerSockJsSession) Test(org.junit.Test)

Example 17 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession in project spring-framework by spring-projects.

the class SockJsClientTests method connectWebSocket.

@Test
public void connectWebSocket() throws Exception {
    setupInfoRequest(true);
    this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback);
    assertTrue(this.webSocketTransport.invoked());
    WebSocketSession session = mock(WebSocketSession.class);
    this.webSocketTransport.getConnectCallback().onSuccess(session);
    verify(this.connectCallback).onSuccess(session);
    verifyNoMoreInteractions(this.connectCallback);
}
Also used : WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 18 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession in project spring-framework by spring-projects.

the class WebSocketTransport method connect.

@Override
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
    final SettableListenableFuture<WebSocketSession> future = new SettableListenableFuture<>();
    WebSocketClientSockJsSession session = new WebSocketClientSockJsSession(request, handler, future);
    handler = new ClientSockJsWebSocketHandler(session);
    request.addTimeoutTask(session.getTimeoutTask());
    URI url = request.getTransportUrl();
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHandshakeHeaders());
    if (logger.isDebugEnabled()) {
        logger.debug("Starting WebSocket session on " + url);
    }
    this.webSocketClient.doHandshake(handler, headers, url).addCallback(new ListenableFutureCallback<WebSocketSession>() {

        @Override
        public void onSuccess(WebSocketSession webSocketSession) {
        // WebSocket session ready, SockJS Session not yet
        }

        @Override
        public void onFailure(Throwable ex) {
            future.setException(ex);
        }
    });
    return future;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) URI(java.net.URI) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 19 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession in project spring-framework by spring-projects.

the class AbstractXhrTransport method connect.

// Transport methods
@Override
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
    SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<>();
    XhrClientSockJsSession session = new XhrClientSockJsSession(request, handler, this, connectFuture);
    request.addTimeoutTask(session.getTimeoutTask());
    URI receiveUrl = request.getTransportUrl();
    if (logger.isDebugEnabled()) {
        logger.debug("Starting XHR " + (isXhrStreamingDisabled() ? "Polling" : "Streaming") + "session url=" + receiveUrl);
    }
    HttpHeaders handshakeHeaders = new HttpHeaders();
    handshakeHeaders.putAll(request.getHandshakeHeaders());
    connectInternal(request, handler, receiveUrl, handshakeHeaders, session, connectFuture);
    return connectFuture;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) URI(java.net.URI) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 20 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession in project spring-framework by spring-projects.

the class SubProtocolWebSocketHandler method checkSessions.

/**
	 * When a session is connected through a higher-level protocol it has a chance
	 * to use heartbeat management to shut down sessions that are too slow to send
	 * or receive messages. However, after a WebSocketSession is established and
	 * before the higher level protocol is fully connected there is a possibility
	 * for sessions to hang. This method checks and closes any sessions that have
	 * been connected for more than 60 seconds without having received a single
	 * message.
	 */
private void checkSessions() throws IOException {
    long currentTime = System.currentTimeMillis();
    if (!isRunning() || (currentTime - this.lastSessionCheckTime < TIME_TO_FIRST_MESSAGE)) {
        return;
    }
    if (this.sessionCheckLock.tryLock()) {
        try {
            for (WebSocketSessionHolder holder : this.sessions.values()) {
                if (holder.hasHandledMessages()) {
                    continue;
                }
                long timeSinceCreated = currentTime - holder.getCreateTime();
                if (timeSinceCreated < TIME_TO_FIRST_MESSAGE) {
                    continue;
                }
                WebSocketSession session = holder.getSession();
                if (logger.isErrorEnabled()) {
                    logger.error("No messages received after " + timeSinceCreated + " ms. " + "Closing " + holder.getSession() + ".");
                }
                try {
                    this.stats.incrementNoMessagesReceivedCount();
                    session.close(CloseStatus.SESSION_NOT_RELIABLE);
                } catch (Throwable ex) {
                    if (logger.isErrorEnabled()) {
                        logger.error("Failure while closing " + session, ex);
                    }
                }
            }
        } finally {
            this.lastSessionCheckTime = currentTime;
            this.sessionCheckLock.unlock();
        }
    }
}
Also used : WebSocketSession(org.springframework.web.socket.WebSocketSession)

Aggregations

WebSocketSession (org.springframework.web.socket.WebSocketSession)32 Test (org.junit.Test)24 TextMessage (org.springframework.web.socket.TextMessage)10 URI (java.net.URI)8 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)3 Callable (java.util.concurrent.Callable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)2 ListenableFutureTask (org.springframework.util.concurrent.ListenableFutureTask)2 SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)2 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)2 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)2 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1