use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class SessionTest method testBasicEcho_FromClient.
@Test
// TODO fix frequent failure
@Ignore
public void testBasicEcho_FromClient() throws Exception {
WebSocketClient client = new WebSocketClient();
client.start();
try {
JettyTrackingSocket cliSock = new JettyTrackingSocket();
client.getPolicy().setIdleTimeout(10000);
URI wsUri = server.getWsUri();
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.setSubProtocols("echo");
Future<Session> future = client.connect(cliSock, wsUri, request);
final IBlockheadServerConnection srvSock = server.accept();
srvSock.upgrade();
Session sess = future.get(30000, TimeUnit.MILLISECONDS);
Assert.assertThat("Session", sess, notNullValue());
Assert.assertThat("Session.open", sess.isOpen(), is(true));
Assert.assertThat("Session.upgradeRequest", sess.getUpgradeRequest(), notNullValue());
Assert.assertThat("Session.upgradeResponse", sess.getUpgradeResponse(), notNullValue());
cliSock.assertWasOpened();
cliSock.assertNotClosed();
Collection<WebSocketSession> sessions = client.getBeans(WebSocketSession.class);
Assert.assertThat("client.connectionManager.sessions.size", sessions.size(), is(1));
RemoteEndpoint remote = cliSock.getSession().getRemote();
remote.sendStringByFuture("Hello World!");
if (remote.getBatchMode() == BatchMode.ON) {
remote.flush();
}
srvSock.echoMessage(1, 30000, TimeUnit.MILLISECONDS);
// wait for response from server
cliSock.waitForMessage(30000, TimeUnit.MILLISECONDS);
Set<WebSocketSession> open = client.getOpenSessions();
Assert.assertThat("(Before Close) Open Sessions.size", open.size(), is(1));
cliSock.assertMessage("Hello World!");
cliSock.close();
srvSock.close();
cliSock.waitForClose(30000, TimeUnit.MILLISECONDS);
open = client.getOpenSessions();
// TODO this sometimes fails!
Assert.assertThat("(After Close) Open Sessions.size", open.size(), is(0));
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class ClientWriteThread method run.
@Override
public void run() {
final AtomicInteger m = new AtomicInteger();
try {
LOG.debug("Writing {} messages to connection {}", messageCount);
LOG.debug("Artificial Slowness {} ms", slowness);
Future<Void> lastMessage = null;
RemoteEndpoint remote = session.getRemote();
while (m.get() < messageCount) {
lastMessage = remote.sendStringByFuture(message + "/" + m.get() + "/");
m.incrementAndGet();
if (slowness > 0) {
TimeUnit.MILLISECONDS.sleep(slowness);
}
}
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
// block on write of last message
if (lastMessage != null)
// block on write
lastMessage.get(2, TimeUnit.MINUTES);
} catch (Exception e) {
LOG.warn(e);
}
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class MyEchoSocket method onWebSocketText.
@Override
public void onWebSocketText(String message) {
if (isNotConnected()) {
return;
}
try {
// echo the data back
RemoteEndpoint remote = getRemote();
remote.sendString(message);
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class BigEchoSocket method onText.
@OnWebSocketMessage
public void onText(Session session, String message) throws IOException {
if (!session.isOpen()) {
LOG.warn("Session is closed");
return;
}
RemoteEndpoint remote = session.getRemote();
remote.sendString(message, null);
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project jetty.project by eclipse.
the class EchoFragmentSocket method onFrame.
@OnWebSocketFrame
public void onFrame(Session session, Frame frame) {
if (!frame.getType().isData()) {
// Don't process non-data frames
return;
}
ByteBuffer data = frame.getPayload();
int half = data.remaining() / 2;
ByteBuffer buf1 = data.slice();
ByteBuffer buf2 = data.slice();
buf1.limit(half);
buf2.position(half);
RemoteEndpoint remote = session.getRemote();
try {
switch(frame.getType()) {
case BINARY:
remote.sendBytes(buf1, null);
remote.sendBytes(buf2, null);
break;
case TEXT:
// NOTE: This impl is not smart enough to split on a UTF8 boundary
remote.sendString(BufferUtil.toUTF8String(buf1), null);
remote.sendString(BufferUtil.toUTF8String(buf2), null);
break;
default:
throw new IOException("Unexpected frame type: " + frame.getType());
}
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
Aggregations