Search in sources :

Example 21 with WebSocket

use of okhttp3.WebSocket in project Rocket.Chat.Android by RocketChat.

the class RxWebSocket method connect.

public ConnectableFlowable<RxWebSocketCallback.Base> connect(String url) {
    final Request request = new Request.Builder().url(url).build();
    return Flowable.create((FlowableOnSubscribe<RxWebSocketCallback.Base>) emitter -> httpClient.newWebSocket(request, new WebSocketListener() {

        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            RxWebSocket.this.webSocket = webSocket;
            emitter.onNext(new RxWebSocketCallback.Open(RxWebSocket.this.webSocket, response));
        }

        @Override
        public void onFailure(WebSocket webSocket, Throwable err, Response response) {
            try {
                emitter.onError(new RxWebSocketCallback.Failure(webSocket, err, response));
            } catch (OnErrorNotImplementedException ex) {
                RCLog.w(ex, "OnErrorNotImplementedException ignored");
            }
        }

        @Override
        public void onMessage(WebSocket webSocket, String text) {
            emitter.onNext(new RxWebSocketCallback.Message(webSocket, text));
        }

        @Override
        public void onClosed(WebSocket webSocket, int code, String reason) {
            emitter.onNext(new RxWebSocketCallback.Close(webSocket, code, reason));
            emitter.onComplete();
        }
    }), BackpressureStrategy.BUFFER).publish();
}
Also used : FlowableOnSubscribe(io.reactivex.FlowableOnSubscribe) WebSocketListener(okhttp3.WebSocketListener) Request(okhttp3.Request) OnErrorNotImplementedException(io.reactivex.exceptions.OnErrorNotImplementedException) WebSocket(okhttp3.WebSocket) Response(okhttp3.Response)

Example 22 with WebSocket

use of okhttp3.WebSocket in project Fast-Android-Networking by amitshekhariitbhu.

the class WebSocketActivity method connectWebSocket.

private void connectWebSocket() {
    OkHttpClient client = new OkHttpClient.Builder().readTimeout(0, TimeUnit.MILLISECONDS).build();
    Request request = new Request.Builder().url("ws://echo.websocket.org").build();
    webSocket = client.newWebSocket(request, getWebSocketListener());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request)

Example 23 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class OkHttpClient method newWebSocket.

/**
   * Uses {@code request} to connect a new web socket.
   */
@Override
public WebSocket newWebSocket(Request request, WebSocketListener listener) {
    RealWebSocket webSocket = new RealWebSocket(request, listener, new SecureRandom());
    webSocket.connect(this);
    return webSocket;
}
Also used : RealWebSocket(okhttp3.internal.ws.RealWebSocket) SecureRandom(java.security.SecureRandom)

Example 24 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method throwingOnFailLogs.

@Ignore("AsyncCall currently lets runtime exceptions propagate.")
@Test
public void throwingOnFailLogs() throws InterruptedException {
    TestLogHandler logs = new TestLogHandler();
    Logger logger = Logger.getLogger(OkHttpClient.class.getName());
    logger.addHandler(logs);
    webServer.enqueue(new MockResponse().setResponseCode(200).setBody("Body"));
    final RuntimeException e = new RuntimeException();
    clientListener.setNextEventDelegate(new WebSocketListener() {

        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            throw e;
        }
    });
    newWebSocket();
    assertEquals("", logs.take());
    logger.removeHandler(logs);
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) WebSocketListener(okhttp3.WebSocketListener) OkHttpClient(okhttp3.OkHttpClient) TestLogHandler(okhttp3.TestLogHandler) Logger(java.util.logging.Logger) WebSocket(okhttp3.WebSocket) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 25 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketRecorder method onClosed.

@Override
public void onClosed(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.onClosed(webSocket, code, reason);
    } else {
        events.add(new Closed(code, reason));
    }
}
Also used : WebSocketListener(okhttp3.WebSocketListener)

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