use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class JettyEchoSocket method onWebSocketBinary.
@Override
public void onWebSocketBinary(byte[] payload, int offset, int len) {
if (isNotConnected())
return;
try {
RemoteEndpoint remote = getRemote();
remote.sendBytes(BufferUtil.toBuffer(payload, offset, len), null);
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
} catch (IOException x) {
throw new RuntimeIOException(x);
}
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class JettyEchoSocket method onWebSocketText.
@Override
public void onWebSocketText(String message) {
if (isNotConnected())
return;
try {
RemoteEndpoint remote = getRemote();
remote.sendString(message, null);
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
} catch (IOException x) {
throw new RuntimeIOException(x);
}
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class WebSocketOverSSLTest method testEcho.
/**
* Test the requirement of issuing socket and receiving echo response
* @throws Exception on test failure
*/
@Test
public void testEcho() throws Exception {
Assert.assertThat("server scheme", server.getServerUri().getScheme(), is("wss"));
WebSocketClient client = new WebSocketClient(server.getSslContextFactory(), null, bufferPool);
try {
client.start();
CaptureSocket clientSocket = new CaptureSocket();
URI requestUri = server.getServerUri();
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
String msg = "this is an echo ... cho ... ho ... o";
RemoteEndpoint remote = session.getRemote();
remote.sendString(msg);
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
// Read frame (hopefully text frame)
clientSocket.messages.awaitEventCount(1, 30, TimeUnit.SECONDS);
EventQueue<String> captured = clientSocket.messages;
Assert.assertThat("Text Message", captured.poll(), is(msg));
// Shutdown the socket
clientSocket.close();
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class WebSocketOverSSLTest method testServerSessionIsSecure.
/**
* Test that server session reports as secure
* @throws Exception on test failure
*/
@Test
public void testServerSessionIsSecure() 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();
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.isSecure");
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
// Read frame (hopefully text frame)
clientSocket.messages.awaitEventCount(1, 30, TimeUnit.SECONDS);
EventQueue<String> captured = clientSocket.messages;
Assert.assertThat("Server.session.isSecure", captured.poll(), is("session.isSecure=true"));
// Shutdown the socket
clientSocket.close();
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class JettyEchoSocket method sendMessage.
public void sendMessage(String msg) throws IOException {
remoteLock.lock();
try {
RemoteEndpoint r = remote;
if (r == null) {
return;
}
r.sendStringByFuture(msg);
if (r.getBatchMode() == BatchMode.ON)
r.flush();
} finally {
remoteLock.unlock();
}
}
Aggregations