Search in sources :

Example 6 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method websocketScheme.

private void websocketScheme(String scheme) throws IOException {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    Request request = new Request.Builder().url(scheme + "://" + webServer.getHostName() + ":" + webServer.getPort() + "/").build();
    RealWebSocket webSocket = newWebSocket(request);
    clientListener.assertOpen();
    serverListener.assertOpen();
    webSocket.send("abc");
    serverListener.assertTextMessage("abc");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest)

Example 7 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketRecorder method onFailure.

@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
    Platform.get().log(Platform.INFO, "[WS " + name + "] onFailure", t);
    WebSocketListener delegate = this.delegate;
    if (delegate != null) {
        this.delegate = null;
        delegate.onFailure(webSocket, t, response);
    } else {
        events.add(new Failure(t, response));
    }
}
Also used : WebSocketListener(okhttp3.WebSocketListener)

Example 8 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketRecorder method onOpen.

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

Example 9 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketRecorder method onClosing.

@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
    Platform.get().log(Platform.INFO, "[WS " + name + "] onClose " + code, null);
    WebSocketListener delegate = this.delegate;
    if (delegate != null) {
        this.delegate = null;
        delegate.onClosing(webSocket, code, reason);
    } else {
        events.add(new Closing(code, reason));
    }
}
Also used : WebSocketListener(okhttp3.WebSocketListener)

Example 10 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method closeReasonTooLong.

@Test
public void closeReasonTooLong() throws IOException {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    WebSocket webSocket = newWebSocket();
    WebSocket server = serverListener.assertOpen();
    clientListener.assertOpen();
    String reason = repeat('X', 124);
    try {
        webSocket.close(1000, reason);
        fail();
    } catch (IllegalArgumentException expected) {
        assertEquals("reason.size() > 123: " + reason, expected.getMessage());
    }
    webSocket.close(1000, null);
    serverListener.assertClosing(1000, "");
    server.close(1000, null);
    clientListener.assertClosing(1000, "");
    clientListener.assertClosed(1000, "");
    serverListener.assertClosed(1000, "");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) 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