Search in sources :

Example 1 with WebSocketListener

use of okhttp3.WebSocketListener 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 2 with WebSocketListener

use of okhttp3.WebSocketListener 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 3 with WebSocketListener

use of okhttp3.WebSocketListener 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 4 with WebSocketListener

use of okhttp3.WebSocketListener in project okhttp by square.

the class WebSocketHttpTest method throwingOnOpenFailsImmediately.

@Test
public void throwingOnOpenFailsImmediately() {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    final RuntimeException e = new RuntimeException();
    clientListener.setNextEventDelegate(new WebSocketListener() {

        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            throw e;
        }
    });
    newWebSocket();
    serverListener.assertOpen();
    serverListener.assertExhausted();
    clientListener.assertFailure(e);
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) WebSocketListener(okhttp3.WebSocketListener) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 5 with WebSocketListener

use of okhttp3.WebSocketListener in project okhttp by square.

the class WebSocketHttpTest method throwingOnClosingClosesImmediatelyAndFails.

@Test
public void throwingOnClosingClosesImmediatelyAndFails() throws IOException {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    final RuntimeException e = new RuntimeException();
    clientListener.setNextEventDelegate(new WebSocketListener() {

        @Override
        public void onClosing(WebSocket webSocket, int code, String reason) {
            throw e;
        }
    });
    server.close(1000, "bye");
    clientListener.assertFailure(e);
    serverListener.assertExhausted();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocketListener(okhttp3.WebSocketListener) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Aggregations

WebSocketListener (okhttp3.WebSocketListener)11 WebSocket (okhttp3.WebSocket)5 MockResponse (okhttp3.mockwebserver.MockResponse)4 Test (org.junit.Test)4 Response (okhttp3.Response)3 ByteString (okio.ByteString)2 FlowableOnSubscribe (io.reactivex.FlowableOnSubscribe)1 OnErrorNotImplementedException (io.reactivex.exceptions.OnErrorNotImplementedException)1 SecureRandom (java.security.SecureRandom)1 Logger (java.util.logging.Logger)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 TestLogHandler (okhttp3.TestLogHandler)1 RealWebSocket (okhttp3.internal.ws.RealWebSocket)1 Ignore (org.junit.Ignore)1