Search in sources :

Example 6 with RemoteEndpoint

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);
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException)

Example 7 with RemoteEndpoint

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);
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException)

Example 8 with RemoteEndpoint

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();
    }
}
Also used : CaptureSocket(org.eclipse.jetty.websocket.server.helper.CaptureSocket) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 9 with RemoteEndpoint

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();
    }
}
Also used : CaptureSocket(org.eclipse.jetty.websocket.server.helper.CaptureSocket) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 10 with RemoteEndpoint

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();
    }
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint)

Aggregations

RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)20 OnWebSocketMessage (org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)7 URI (java.net.URI)6 Session (org.eclipse.jetty.websocket.api.Session)6 Test (org.junit.Test)6 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)3 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)3 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)3 CaptureSocket (org.eclipse.jetty.websocket.server.helper.CaptureSocket)3 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)2 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 WebSocketAdapter (org.eclipse.jetty.websocket.api.WebSocketAdapter)1 OnWebSocketFrame (org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)1 Ignore (org.junit.Ignore)1