Search in sources :

Example 1 with PingMessage

use of org.springframework.web.socket.PingMessage in project ma-core-public by infiniteautomation.

the class MangoPingPongTracker method sendPing.

public void sendPing() {
    try {
        session.getAttributes().put(MangoWebSocketPublisher.RECEIVED_PONG, Boolean.FALSE);
        session.sendMessage(new PingMessage());
    } catch (IOException | WebSocketException e) {
        if (log.isErrorEnabled()) {
            log.error("Error sending websocket ping", e);
        }
    } finally {
        task = new TimeoutTask(this.timeout, this);
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) IOException(java.io.IOException) PingMessage(org.springframework.web.socket.PingMessage) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Example 2 with PingMessage

use of org.springframework.web.socket.PingMessage in project spring-integration by spring-projects.

the class ClientWebSocketContainerTests method testClientWebSocketContainer.

@Test
public void testClientWebSocketContainer() throws Exception {
    final AtomicBoolean failure = new AtomicBoolean();
    StandardWebSocketClient webSocketClient = new StandardWebSocketClient() {

        @Override
        protected ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler webSocketHandler, HttpHeaders headers, URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
            ListenableFuture<WebSocketSession> future = super.doHandshakeInternal(webSocketHandler, headers, uri, protocols, extensions, attributes);
            if (failure.get()) {
                future.cancel(true);
            }
            return future;
        }
    };
    Map<String, Object> userProperties = new HashMap<String, Object>();
    userProperties.put(Constants.IO_TIMEOUT_MS_PROPERTY, "" + (Constants.IO_TIMEOUT_MS_DEFAULT * 6));
    webSocketClient.setUserProperties(userProperties);
    ClientWebSocketContainer container = new ClientWebSocketContainer(webSocketClient, server.getWsBaseUrl() + "/ws/websocket");
    TestWebSocketListener messageListener = new TestWebSocketListener();
    container.setMessageListener(messageListener);
    container.setConnectionTimeout(30);
    container.start();
    WebSocketSession session = container.getSession(null);
    assertNotNull(session);
    assertTrue(session.isOpen());
    assertEquals("v10.stomp", session.getAcceptedProtocol());
    session.sendMessage(new PingMessage());
    assertTrue(messageListener.messageLatch.await(10, TimeUnit.SECONDS));
    container.stop();
    try {
        container.getSession(null);
        fail("IllegalStateException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(IllegalStateException.class));
        assertEquals(e.getMessage(), "'clientSession' has not been established. Consider to 'start' this container.");
    }
    assertTrue(messageListener.sessionEndedLatch.await(10, TimeUnit.SECONDS));
    assertFalse(session.isOpen());
    assertTrue(messageListener.started);
    assertThat(messageListener.message, instanceOf(PongMessage.class));
    failure.set(true);
    container.start();
    try {
        container.getSession(null);
        fail("IllegalStateException is expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(IllegalStateException.class));
        assertThat(e.getCause(), instanceOf(CancellationException.class));
    }
    failure.set(false);
    container.start();
    session = container.getSession(null);
    assertNotNull(session);
    assertTrue(session.isOpen());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HashMap(java.util.HashMap) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) URI(java.net.URI) PingMessage(org.springframework.web.socket.PingMessage) CancellationException(java.util.concurrent.CancellationException) WebSocketSession(org.springframework.web.socket.WebSocketSession) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) List(java.util.List) PongMessage(org.springframework.web.socket.PongMessage) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with PingMessage

use of org.springframework.web.socket.PingMessage in project spring-boot by spring-projects.

the class LiveReloadServerTests method pingPong.

@Test
void pingPong() throws Exception {
    LiveReloadWebSocketHandler handler = connect();
    handler.sendMessage(new PingMessage());
    await().atMost(Duration.ofSeconds(10)).until(handler::getPongCount, is(1));
}
Also used : PingMessage(org.springframework.web.socket.PingMessage) Test(org.junit.jupiter.api.Test)

Aggregations

PingMessage (org.springframework.web.socket.PingMessage)3 TimeoutTask (com.serotonin.m2m2.util.timeout.TimeoutTask)1 IOException (java.io.IOException)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CancellationException (java.util.concurrent.CancellationException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 HttpHeaders (org.springframework.http.HttpHeaders)1 PongMessage (org.springframework.web.socket.PongMessage)1 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)1 WebSocketSession (org.springframework.web.socket.WebSocketSession)1 StandardWebSocketClient (org.springframework.web.socket.client.standard.StandardWebSocketClient)1