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();
}
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));
}
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;
}
}
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();
}
}
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();
}
}
Aggregations