use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.
the class CdiAppTest method testWebSocketActivated.
@Test
public void testWebSocketActivated() throws Exception {
WebSocketClient client = new WebSocketClient();
try {
client.start();
CheckSocket socket = new CheckSocket();
client.connect(socket, serverWebsocketURI.resolve("/echo"));
socket.awaitOpen(2, TimeUnit.SECONDS);
socket.sendText("Hello");
socket.close(StatusCode.NORMAL, "Test complete");
socket.awaitClose(2, TimeUnit.SECONDS);
assertThat("Messages received", socket.getTextMessages().size(), is(1));
assertThat("Message[0]", socket.getTextMessages().poll(), is("Hello"));
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.
the class TestClient method main.
public static void main(String[] args) throws Exception {
String host = "localhost";
int port = 8080;
String protocol = null;
int count = 10;
int size = 64;
int fragment = 4000;
boolean binary = false;
int clients = 1;
int delay = 1000;
for (int i = 0; i < args.length; i++) {
String a = args[i];
if ("-p".equals(a) || "--port".equals(a)) {
port = Integer.parseInt(args[++i]);
} else if ("-h".equals(a) || "--host".equals(a)) {
host = args[++i];
} else if ("-c".equals(a) || "--count".equals(a)) {
count = Integer.parseInt(args[++i]);
} else if ("-s".equals(a) || "--size".equals(a)) {
size = Integer.parseInt(args[++i]);
} else if ("-f".equals(a) || "--fragment".equals(a)) {
fragment = Integer.parseInt(args[++i]);
} else if ("-P".equals(a) || "--protocol".equals(a)) {
protocol = args[++i];
} else if ("-v".equals(a) || "--verbose".equals(a)) {
_verbose = true;
} else if ("-b".equals(a) || "--binary".equals(a)) {
binary = true;
} else if ("-C".equals(a) || "--clients".equals(a)) {
clients = Integer.parseInt(args[++i]);
} else if ("-d".equals(a) || "--delay".equals(a)) {
delay = Integer.parseInt(args[++i]);
} else if (a.startsWith("-")) {
usage(args);
}
}
TestClient[] client = new TestClient[clients];
WebSocketClient wsclient = new WebSocketClient(bufferPool);
try {
wsclient.start();
__start = System.currentTimeMillis();
protocol = protocol == null ? "echo" : protocol;
for (int i = 0; i < clients; i++) {
client[i] = new TestClient(wsclient, host, port, protocol, 60000);
client[i].open();
}
System.out.println("Jetty WebSocket PING " + host + ":" + port + " (" + new InetSocketAddress(host, port) + ") " + clients + " clients " + protocol);
for (int p = 0; p < count; p++) {
long next = System.currentTimeMillis() + delay;
byte op = OpCode.TEXT;
if (binary) {
op = OpCode.BINARY;
}
byte[] data = null;
switch(op) {
case OpCode.TEXT:
{
StringBuilder b = new StringBuilder();
while (b.length() < size) {
b.append('A' + __random.nextInt(26));
}
data = b.toString().getBytes(StandardCharsets.UTF_8);
break;
}
case OpCode.BINARY:
{
data = new byte[size];
__random.nextBytes(data);
break;
}
}
for (int i = 0; i < clients; i++) {
client[i].send(op, data, fragment);
}
while (System.currentTimeMillis() < next) {
Thread.sleep(10);
}
}
} finally {
for (int i = 0; i < clients; i++) {
if (client[i] != null) {
client[i].disconnect();
}
}
long duration = System.currentTimeMillis() - __start;
System.out.println("--- " + host + " websocket ping statistics using " + clients + " connection" + (clients > 1 ? "s" : "") + " ---");
System.out.printf("%d/%d frames sent/recv, %d/%d mesg sent/recv, time %dms %dm/s %.2fbps%n", __framesSent, __framesReceived.get(), __messagesSent, __messagesReceived.get(), duration, ((1000L * __messagesReceived.get()) / duration), (1000.0D * __messagesReceived.get() * 8 * size) / duration / 1024 / 1024);
System.out.printf("rtt min/ave/max = %.3f/%.3f/%.3f ms\n", __minDuration.get() / 1000000.0, __messagesReceived.get() == 0 ? 0.0 : (__totalTime.get() / __messagesReceived.get() / 1000000.0), __maxDuration.get() / 1000000.0);
wsclient.stop();
}
bufferPool.assertNoLeaks();
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project nanohttpd by NanoHttpd.
the class EchoWebSocketsTest method testWebsocketClient.
@Test
public void testWebsocketClient() throws Exception {
String destUri = "ws://localhost:9191";
WebSocketClient client = new WebSocketClient();
SimpleEchoSocket socket = new SimpleEchoSocket();
socket.getToSendMessages().add("Hello");
socket.getToSendMessages().add("Thanks for the conversation.");
socket.getToSendMessages().add(createString(31000));
socket.getToSendMessages().add(createString(65400));
try {
client.start();
URI echoUri = new URI(destUri);
ClientUpgradeRequest request = new ClientUpgradeRequest();
client.connect(socket, echoUri, request);
System.out.printf("Connecting to : %s%n", echoUri);
socket.awaitClose(5, TimeUnit.SECONDS);
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
client.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
Assert.assertEquals(4, socket.getReceivedMessages().size());
Assert.assertEquals("Hello", socket.getReceivedMessages().get(0));
Assert.assertEquals("Thanks for the conversation.", socket.getReceivedMessages().get(1));
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project neo4j by neo4j.
the class WebSocketConnectionTest method shouldThrowIOExceptionWhenNotEnoughDataReceivedBeforeClose.
@Test
public void shouldThrowIOExceptionWhenNotEnoughDataReceivedBeforeClose() throws Throwable {
// Given
WebSocketClient client = mock(WebSocketClient.class);
WebSocketConnection conn = new WebSocketConnection(client);
when(client.isStopped()).thenReturn(true, true);
byte[] data = { 0, 1, 2, 3 };
// When && Then
conn.onWebSocketBinary(data, 0, 4);
expectedException.expect(IOException.class);
expectedException.expectMessage("Connection closed while waiting for data from the server.");
conn.recv(10);
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project gocd by gocd.
the class GoAgentServerWebSocketClientBuilder method build.
@Override
public WebSocketClient build() throws Exception {
SslContextFactory sslContextFactory = sslVerificationMode == SslVerificationMode.NONE ? new TrustAllSSLContextFactory() : new SslContextFactory();
sslContextFactory.setNeedClientAuth(true);
sslContextFactory.setKeyStore(agentKeystore());
sslContextFactory.setKeyStorePassword(keystorePassword());
sslContextFactory.setKeyManagerPassword(keystorePassword());
if (rootCertFile != null) {
sslContextFactory.setTrustStore(agentTruststore());
sslContextFactory.setTrustStorePassword(keystorePassword());
}
if (sslVerificationMode == SslVerificationMode.NO_VERIFY_HOST) {
sslContextFactory.setEndpointIdentificationAlgorithm(null);
}
WebSocketClient client = new WebSocketClient(sslContextFactory);
client.setMaxIdleTimeout(systemEnvironment.getWebsocketMaxIdleTime());
return client;
}
Aggregations