Search in sources :

Example 61 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method readTimeoutAppliesToHttpRequest.

@Test
public void readTimeoutAppliesToHttpRequest() {
    webServer.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    WebSocket webSocket = newWebSocket();
    clientListener.assertFailure(SocketTimeoutException.class, "timeout", "Read timed out");
    assertThat(webSocket.close(1000, null)).isFalse();
}
Also used : MockResponse(mockwebserver3.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 62 with WebSocket

use of okhttp3.WebSocket in project java-sdk by watson-developer-cloud.

the class SpeechToTextTest method testClosingInputStreamClosesWebSocket.

@Test
public void testClosingInputStreamClosesWebSocket() throws Exception {
    TestRecognizeCallback callback = new TestRecognizeCallback();
    WebSocketRecorder webSocketRecorder = new WebSocketRecorder("server");
    PipedOutputStream outputStream = new PipedOutputStream();
    InputStream inputStream = new PipedInputStream(outputStream);
    server.enqueue(new MockResponse().withWebSocketUpgrade(webSocketRecorder));
    String customizationId = "id";
    String version = "version";
    Double customizationWeight = 0.1;
    RecognizeOptions options = new RecognizeOptions.Builder().audio(inputStream).contentType(HttpMediaType.createAudioRaw(44000)).customizationId(customizationId).version(version).customizationWeight(customizationWeight).build();
    service.recognizeUsingWebSocket(options, callback);
    WebSocket serverSocket = webSocketRecorder.assertOpen();
    serverSocket.send("{\"state\": {}}");
    outputStream.write(ByteString.encodeUtf8("test").toByteArray());
    outputStream.close();
    webSocketRecorder.assertTextMessage("{\"content-type\":\"audio/l16; rate=44000\"," + "\"action\":\"start\"}");
    webSocketRecorder.assertBinaryMessage(ByteString.encodeUtf8("test"));
    webSocketRecorder.assertTextMessage("{\"action\":\"stop\"}");
    webSocketRecorder.assertExhausted();
    serverSocket.close(1000, null);
    callback.assertConnected();
    callback.assertDisconnected();
    callback.assertNoErrors();
    callback.assertOnTranscriptionComplete();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) PipedInputStream(java.io.PipedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteString(okio.ByteString) WebSocketRecorder(okhttp3.internal.ws.WebSocketRecorder) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions) WebSocket(okhttp3.WebSocket) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 63 with WebSocket

use of okhttp3.WebSocket in project java-sdk by watson-developer-cloud.

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 64 with WebSocket

use of okhttp3.WebSocket in project java-sdk by watson-developer-cloud.

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 65 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)

Aggregations

WebSocket (okhttp3.WebSocket)42 MockResponse (mockwebserver3.MockResponse)27 Test (org.junit.jupiter.api.Test)27 WebSocketListener (okhttp3.WebSocketListener)25 ByteString (okio.ByteString)20 Request (okhttp3.Request)16 Response (okhttp3.Response)9 OkHttpClient (okhttp3.OkHttpClient)8 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)4 RecordedRequest (mockwebserver3.RecordedRequest)3 Single (io.reactivex.rxjava3.core.Single)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 InputStream (java.io.InputStream)2 StandardCharsets (java.nio.charset.StandardCharsets)2