Search in sources :

Example 11 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project gocd by gocd.

the class WebSocketClientHandler method connect.

public Session connect(AgentWebSocketClientController controller) throws Exception {
    if (webSocketClient == null || !webSocketClient.isRunning()) {
        if (webSocketClient != null) {
            webSocketClient.stop();
        }
        webSocketClient = builder.build();
        webSocketClient.start();
    }
    LOG.info("Connecting to websocket endpoint: " + urlService.getAgentRemoteWebSocketUrl());
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.addExtensions("fragment;maxLength=" + getMessageBufferSize());
    return webSocketClient.connect(controller, new URI(urlService.getAgentRemoteWebSocketUrl()), request).get();
}
Also used : ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) URI(java.net.URI)

Example 12 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project spark by perwendel.

the class GenericIntegrationTest method testWebSocketConversation.

@Test
public void testWebSocketConversation() throws Exception {
    String uri = "ws://localhost:4567/ws";
    WebSocketClient client = new WebSocketClient();
    WebSocketTestClient ws = new WebSocketTestClient();
    try {
        client.start();
        client.connect(ws, URI.create(uri), new ClientUpgradeRequest());
        ws.awaitClose(30, TimeUnit.SECONDS);
    } finally {
        client.stop();
    }
    List<String> events = WebSocketTestHandler.events;
    Assert.assertEquals(3, events.size(), 3);
    Assert.assertEquals("onConnect", events.get(0));
    Assert.assertEquals("onMessage: Hi Spark!", events.get(1));
    Assert.assertEquals("onClose: 1000 Bye!", events.get(2));
}
Also used : WebSocketTestClient(spark.embeddedserver.jetty.websocket.WebSocketTestClient) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Test(org.junit.Test)

Example 13 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project spring-framework by spring-projects.

the class JettyWebSocketClient method doHandshakeInternal.

@Override
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
    final ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols(protocols);
    for (WebSocketExtension e : extensions) {
        request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e));
    }
    for (String header : headers.keySet()) {
        request.setHeader(header, headers.get(header));
    }
    Principal user = getUser();
    final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
    final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);
    Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {

        @Override
        public WebSocketSession call() throws Exception {
            Future<Session> future = client.connect(listener, uri, request);
            future.get();
            return wsSession;
        }
    };
    if (this.taskExecutor != null) {
        return this.taskExecutor.submitListenable(connectTask);
    } else {
        ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
        task.run();
        return task;
    }
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) Callable(java.util.concurrent.Callable) WebSocketSession(org.springframework.web.socket.WebSocketSession) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) WebSocketToJettyExtensionConfigAdapter(org.springframework.web.socket.adapter.jetty.WebSocketToJettyExtensionConfigAdapter) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Principal(java.security.Principal) WebSocketSession(org.springframework.web.socket.WebSocketSession) Session(org.eclipse.jetty.websocket.api.Session) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession)

Example 14 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project pulsar by yahoo.

the class ProxyAuthenticationTest method socketTest.

@Test
public void socketTest() throws InterruptedException {
    URI consumeUri = URI.create(CONSUME_URI);
    URI produceUri = URI.create(PRODUCE_URI);
    WebSocketClient consumeClient = new WebSocketClient();
    SimpleConsumerSocket consumeSocket = new SimpleConsumerSocket();
    WebSocketClient produceClient = new WebSocketClient();
    SimpleProducerSocket produceSocket = new SimpleProducerSocket();
    try {
        consumeClient.start();
        ClientUpgradeRequest consumeRequest = new ClientUpgradeRequest();
        Future<Session> consumerFuture = consumeClient.connect(consumeSocket, consumeUri, consumeRequest);
        log.info("Connecting to : {}", consumeUri);
        ClientUpgradeRequest produceRequest = new ClientUpgradeRequest();
        produceClient.start();
        Future<Session> producerFuture = produceClient.connect(produceSocket, produceUri, produceRequest);
        // let it connect
        Thread.sleep(1000);
        Assert.assertTrue(consumerFuture.get().isOpen());
        Assert.assertTrue(producerFuture.get().isOpen());
        consumeSocket.awaitClose(1, TimeUnit.SECONDS);
        produceSocket.awaitClose(1, TimeUnit.SECONDS);
        Assert.assertTrue(produceSocket.getBuffer().size() > 0);
        Assert.assertEquals(produceSocket.getBuffer(), consumeSocket.getBuffer());
    } catch (Throwable t) {
        log.error(t.getMessage());
    } finally {
        ExecutorService executor = newFixedThreadPool(1);
        try {
            executor.submit(() -> {
                try {
                    consumeClient.stop();
                    produceClient.stop();
                    log.info("proxy clients are stopped successfully");
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }).get(2, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("failed to close clients ", e);
        }
        executor.shutdownNow();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.testng.annotations.Test)

Example 15 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project pulsar by yahoo.

the class ProxyPublishConsumeTest method socketTest.

@Test(timeOut = 10000)
public void socketTest() throws Exception {
    URI consumeUri = URI.create(CONSUME_URI);
    URI produceUri = URI.create(PRODUCE_URI);
    WebSocketClient consumeClient = new WebSocketClient();
    SimpleConsumerSocket consumeSocket = new SimpleConsumerSocket();
    WebSocketClient produceClient = new WebSocketClient();
    SimpleProducerSocket produceSocket = new SimpleProducerSocket();
    try {
        consumeClient.start();
        ClientUpgradeRequest consumeRequest = new ClientUpgradeRequest();
        Future<Session> consumerFuture = consumeClient.connect(consumeSocket, consumeUri, consumeRequest);
        log.info("Connecting to : {}", consumeUri);
        ClientUpgradeRequest produceRequest = new ClientUpgradeRequest();
        produceClient.start();
        Future<Session> producerFuture = produceClient.connect(produceSocket, produceUri, produceRequest);
        // let it connect
        Assert.assertTrue(consumerFuture.get().isOpen());
        Assert.assertTrue(producerFuture.get().isOpen());
        while (consumeSocket.getReceivedMessagesCount() < 10) {
            Thread.sleep(10);
        }
        Assert.assertTrue(produceSocket.getBuffer().size() > 0);
        Assert.assertEquals(produceSocket.getBuffer(), consumeSocket.getBuffer());
    } finally {
        ExecutorService executor = newFixedThreadPool(1);
        try {
            executor.submit(() -> {
                try {
                    consumeClient.stop();
                    produceClient.stop();
                    log.info("proxy clients are stopped successfully");
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }).get(2, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("failed to close clients ", e);
        }
        executor.shutdownNow();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.testng.annotations.Test)

Aggregations

ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)17 URI (java.net.URI)10 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)10 Session (org.eclipse.jetty.websocket.api.Session)9 IOException (java.io.IOException)5 ExecutorService (java.util.concurrent.ExecutorService)5 Test (org.testng.annotations.Test)4 ExecutionException (java.util.concurrent.ExecutionException)3 Test (org.junit.Test)3 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ParameterException (com.beust.jcommander.ParameterException)1 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 HttpCookie (java.net.HttpCookie)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1