Search in sources :

Example 11 with WebSocketListener

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

use of okhttp3.WebSocketListener in project okhttp by square.

the class WebSocketHttpTest method throwingOnMessageClosesImmediatelyAndFails.

@Test
public void throwingOnMessageClosesImmediatelyAndFails() 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 onMessage(WebSocket webSocket, String text) {
            throw e;
        }
    });
    server.send("Hello, WebSockets!");
    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