Search in sources :

Example 1 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession in project pinpoint by naver.

the class ActiveThreadCountResponseAggregator method flush0.

private void flush0(TextMessage webSocketMessage) {
    for (WebSocketSession webSocketSession : webSocketSessions) {
        try {
            logger.debug("flush webSocketSession:{}, response:{}", webSocketSession, webSocketMessage);
            webSocketSession.sendMessage(webSocketMessage);
        } catch (Exception e) {
            logger.warn("failed while flushing message to webSocket. session:{}, message:{}, error:{}", webSocketSession, webSocketMessage, e.getMessage(), e);
        }
    }
}
Also used : JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 2 with WebSocketSession

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

the class SubProtocolWebSocketHandler method handleMessage.

/**
	 * Handle an outbound Spring Message to a WebSocket client.
	 */
@Override
public void handleMessage(Message<?> message) throws MessagingException {
    String sessionId = resolveSessionId(message);
    if (sessionId == null) {
        if (logger.isErrorEnabled()) {
            logger.error("Couldn't find session id in " + message);
        }
        return;
    }
    WebSocketSessionHolder holder = this.sessions.get(sessionId);
    if (holder == null) {
        if (logger.isDebugEnabled()) {
            // The broker may not have removed the session yet
            logger.debug("No session for " + message);
        }
        return;
    }
    WebSocketSession session = holder.getSession();
    try {
        findProtocolHandler(session).handleMessageToClient(session, message);
    } catch (SessionLimitExceededException ex) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Terminating '" + session + "'", ex);
            }
            this.stats.incrementLimitExceededCount();
            // clear first, session may be unresponsive
            clearSession(session, ex.getStatus());
            session.close(ex.getStatus());
        } catch (Exception secondException) {
            logger.debug("Failure while closing session " + sessionId + ".", secondException);
        }
    } catch (Exception ex) {
        // Could be part of normal workflow (e.g. browser tab closed)
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to send message to client in " + session + ": " + message, ex);
        }
    }
}
Also used : SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) MessagingException(org.springframework.messaging.MessagingException) SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) IOException(java.io.IOException) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 3 with WebSocketSession

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

the class StandardWebSocketClientTests method testGetRemoteAddress.

@Test
public void testGetRemoteAddress() throws Exception {
    URI uri = new URI("wss://localhost/abc");
    WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
    assertNotNull(session.getRemoteAddress());
    assertEquals("localhost", session.getRemoteAddress().getHostName());
    assertEquals(443, session.getLocalAddress().getPort());
}
Also used : URI(java.net.URI) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 4 with WebSocketSession

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

the class StandardWebSocketClientTests method handshakeHeaders.

@Test
public void handshakeHeaders() throws Exception {
    URI uri = new URI("ws://localhost/abc");
    List<String> protocols = Collections.singletonList("abc");
    this.headers.setSecWebSocketProtocol(protocols);
    this.headers.add("foo", "bar");
    WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
    assertEquals(1, session.getHandshakeHeaders().size());
    assertEquals("bar", session.getHandshakeHeaders().getFirst("foo"));
}
Also used : URI(java.net.URI) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 5 with WebSocketSession

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

the class WebSocketConfigurationTests method registerWebSocketHandler.

@Test
public void registerWebSocketHandler() throws Exception {
    WebSocketSession session = this.webSocketClient.doHandshake(new AbstractWebSocketHandler() {
    }, getWsBaseUrl() + "/ws").get();
    TestHandler serverHandler = this.wac.getBean(TestHandler.class);
    assertTrue(serverHandler.connectLatch.await(2, TimeUnit.SECONDS));
    session.close();
}
Also used : AbstractWebSocketHandler(org.springframework.web.socket.handler.AbstractWebSocketHandler) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

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