Search in sources :

Example 26 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketRecorder method onMessage.

@Override
public void onMessage(WebSocket webSocket, String text) {
    Platform.get().log(Platform.INFO, "[WS " + name + "] onMessage", null);
    WebSocketListener delegate = this.delegate;
    if (delegate != null) {
        this.delegate = null;
        delegate.onMessage(webSocket, text);
    } else {
        Message event = new Message(text);
        events.add(event);
    }
}
Also used : WebSocketListener(okhttp3.WebSocketListener)

Example 27 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketRecorder method onMessage.

@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
    Platform.get().log(Platform.INFO, "[WS " + name + "] onMessage", null);
    WebSocketListener delegate = this.delegate;
    if (delegate != null) {
        this.delegate = null;
        delegate.onMessage(webSocket, bytes);
    } else {
        Message event = new Message(bytes);
        events.add(event);
    }
}
Also used : WebSocketListener(okhttp3.WebSocketListener)

Example 28 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method readTimeoutAppliesToHttpRequest.

@Test
public void readTimeoutAppliesToHttpRequest() throws IOException {
    webServer.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    WebSocket webSocket = newWebSocket();
    clientListener.assertFailure(SocketTimeoutException.class, "timeout");
    assertFalse(webSocket.close(1000, null));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 29 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method clientCancelsIfCloseIsNotAcknowledged.

/** https://github.com/square/okhttp/issues/2788 */
@Test
public void clientCancelsIfCloseIsNotAcknowledged() throws Exception {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    RealWebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    // Initiate a close on the client, which will schedule a hard cancel in 500 ms.
    long closeAtNanos = System.nanoTime();
    webSocket.close(1000, "goodbye", 500);
    serverListener.assertClosing(1000, "goodbye");
    // Confirm that the hard cancel occurred after 500 ms.
    clientListener.assertFailure();
    long elapsedUntilFailure = System.nanoTime() - closeAtNanos;
    assertEquals(500, TimeUnit.NANOSECONDS.toMillis(elapsedUntilFailure), 250d);
    // Close the server and confirm it saw what we expected.
    server.close(1000, null);
    serverListener.assertClosed(1000, "goodbye");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 30 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method binaryMessage.

@Test
public void binaryMessage() throws IOException {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    serverListener.assertOpen();
    webSocket.send(ByteString.encodeUtf8("Hello!"));
    serverListener.assertBinaryMessage(ByteString.of(new byte[] { 'H', 'e', 'l', 'l', 'o', '!' }));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)23 Test (org.junit.Test)22 WebSocket (okhttp3.WebSocket)20 WebSocketListener (okhttp3.WebSocketListener)11 Response (okhttp3.Response)7 ByteString (okio.ByteString)7 Request (okhttp3.Request)5 IOException (java.io.IOException)4 SecureRandom (java.security.SecureRandom)2 Interceptor (okhttp3.Interceptor)2 OkHttpClient (okhttp3.OkHttpClient)2 RealWebSocket (okhttp3.internal.ws.RealWebSocket)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 FlowableOnSubscribe (io.reactivex.FlowableOnSubscribe)1 OnErrorNotImplementedException (io.reactivex.exceptions.OnErrorNotImplementedException)1 InterruptedIOException (java.io.InterruptedIOException)1 ProtocolException (java.net.ProtocolException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Logger (java.util.logging.Logger)1