Search in sources :

Example 26 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.

the class OnMessageReturnTest method testEchoReturn.

@Test
public void testEchoReturn() throws Exception {
    WSServer wsb = new WSServer(testdir, "app");
    wsb.copyWebInf("empty-web.xml");
    wsb.copyClass(EchoReturnEndpoint.class);
    try {
        wsb.start();
        URI uri = wsb.getServerBaseURI();
        WebAppContext webapp = wsb.createWebAppContext();
        wsb.deployWebapp(webapp);
        WebSocketClient client = new WebSocketClient(bufferPool);
        try {
            client.start();
            JettyEchoSocket clientEcho = new JettyEchoSocket();
            Future<Session> future = client.connect(clientEcho, uri.resolve("echoreturn"));
            // wait for connect
            future.get(1, TimeUnit.SECONDS);
            clientEcho.sendMessage("Hello World");
            Queue<String> msgs = clientEcho.awaitMessages(1);
            Assert.assertEquals("Expected message", "Hello World", msgs.poll());
        } finally {
            client.stop();
        }
    } finally {
        wsb.stop();
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 27 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.

the class IdleTimeoutTest method assertConnectionTimeout.

private void assertConnectionTimeout(URI uri) throws Exception, IOException, InterruptedException, ExecutionException, TimeoutException {
    WebSocketClient client = new WebSocketClient(bufferPool);
    try {
        client.start();
        JettyEchoSocket clientEcho = new JettyEchoSocket();
        if (LOG.isDebugEnabled())
            LOG.debug("Client Attempting to connnect");
        Future<Session> future = client.connect(clientEcho, uri);
        // wait for connect
        future.get(1, TimeUnit.SECONDS);
        if (LOG.isDebugEnabled())
            LOG.debug("Client Connected");
        // wait 1 second
        if (LOG.isDebugEnabled())
            LOG.debug("Waiting 1 second");
        TimeUnit.SECONDS.sleep(1);
        if (LOG.isDebugEnabled())
            LOG.debug("Waited 1 second");
        if (clientEcho.getClosed() == false) {
            // Try to write
            clientEcho.sendMessage("You shouldn't be there");
            try {
                Queue<String> msgs = clientEcho.awaitMessages(1);
                assertThat("Should not have received messages echoed back", msgs, is(empty()));
            } catch (TimeoutException | InterruptedException e) {
            // valid success path
            }
        }
    } finally {
        client.stop();
    }
}
Also used : WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Session(org.eclipse.jetty.websocket.api.Session) TimeoutException(java.util.concurrent.TimeoutException)

Example 28 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.

the class SessionTest method assertResponse.

private void assertResponse(String requestPath, String requestMessage, String expectedResponse) throws Exception {
    WebSocketClient client = new WebSocketClient(bufferPool);
    try {
        client.start();
        JettyEchoSocket clientEcho = new JettyEchoSocket();
        Future<Session> future = client.connect(clientEcho, serverUri.resolve(requestPath));
        // wait for connect
        future.get(1, TimeUnit.SECONDS);
        clientEcho.sendMessage(requestMessage);
        Queue<String> msgs = clientEcho.awaitMessages(1);
        Assert.assertThat("Expected message", msgs.poll(), is(expectedResponse));
    } finally {
        client.stop();
    }
}
Also used : WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Session(org.eclipse.jetty.websocket.api.Session)

Example 29 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.

the class WebSocketOverSSLTest method testServerSessionRequestURI.

/**
     * Test that server session.upgradeRequest.requestURI reports correctly
     * @throws Exception on test failure
     */
@Test
public void testServerSessionRequestURI() throws Exception {
    Assert.assertThat("server scheme", server.getServerUri().getScheme(), is("wss"));
    WebSocketClient client = new WebSocketClient(server.getSslContextFactory(), null, bufferPool);
    try {
        client.setConnectTimeout(CONNECT_TIMEOUT);
        client.start();
        CaptureSocket clientSocket = new CaptureSocket();
        URI requestUri = server.getServerUri().resolve("/deep?a=b");
        System.err.printf("Request URI: %s%n", requestUri.toASCIIString());
        Future<Session> fut = client.connect(clientSocket, requestUri);
        // wait for connect
        Session session = fut.get(FUTURE_TIMEOUT_SEC, TimeUnit.SECONDS);
        // Generate text frame
        RemoteEndpoint remote = session.getRemote();
        remote.sendString("session.upgradeRequest.requestURI");
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
        // Read frame (hopefully text frame)
        clientSocket.messages.awaitEventCount(1, 30, TimeUnit.SECONDS);
        EventQueue<String> captured = clientSocket.messages;
        String expected = String.format("session.upgradeRequest.requestURI=%s", requestUri.toASCIIString());
        Assert.assertThat("session.upgradeRequest.requestURI", captured.poll(), is(expected));
        // Shutdown the socket
        clientSocket.close();
    } finally {
        client.stop();
    }
}
Also used : CaptureSocket(org.eclipse.jetty.websocket.server.helper.CaptureSocket) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 30 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.

the class BatchModeTest method prepare.

@Before
public void prepare() throws Exception {
    server = new Server();
    connector = new ServerConnector(server);
    server.addConnector(connector);
    WebSocketHandler handler = new WebSocketHandler() {

        @Override
        public void configure(WebSocketServletFactory factory) {
            factory.register(EchoSocket.class);
        }
    };
    server.setHandler(handler);
    client = new WebSocketClient();
    server.addBean(client, true);
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) Server(org.eclipse.jetty.server.Server) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Before(org.junit.Before)

Aggregations

WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)40 Test (org.junit.Test)26 URI (java.net.URI)25 Session (org.eclipse.jetty.websocket.api.Session)23 HashMap (java.util.HashMap)10 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)10 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)9 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)9 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)9 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)9 MapPropertySource (org.springframework.core.env.MapPropertySource)9 StandardEnvironment (org.springframework.core.env.StandardEnvironment)9 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9 ServiceError (com.kixeye.chassis.transport.dto.ServiceError)8 JsonJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe)8 XmlMessageSerDe (com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe)8 YamlJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe)8 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)8 Envelope (com.kixeye.chassis.transport.dto.Envelope)7 Header (com.kixeye.chassis.transport.dto.Header)6