Search in sources :

Example 1 with RealWebSocket

use of okhttp3.internal.ws.RealWebSocket in project okhttp by square.

the class MockWebServer method handleWebSocketUpgrade.

private void handleWebSocketUpgrade(Socket socket, BufferedSource source, BufferedSink sink, RecordedRequest request, MockResponse response) throws IOException {
    String key = request.getHeader("Sec-WebSocket-Key");
    response.setHeader("Sec-WebSocket-Accept", WebSocketProtocol.acceptHeader(key));
    writeHttpResponse(socket, sink, response);
    // Adapt the request and response into our Request and Response domain model.
    String scheme = request.getTlsVersion() != null ? "https" : "http";
    // Has host and port.
    String authority = request.getHeader("Host");
    final Request fancyRequest = new Request.Builder().url(scheme + "://" + authority + "/").headers(request.getHeaders()).build();
    final Response fancyResponse = new Response.Builder().code(Integer.parseInt(response.getStatus().split(" ")[1])).message(response.getStatus().split(" ", 3)[2]).headers(response.getHeaders()).request(fancyRequest).protocol(Protocol.HTTP_1_1).build();
    final CountDownLatch connectionClose = new CountDownLatch(1);
    RealWebSocket.Streams streams = new RealWebSocket.Streams(false, source, sink) {

        @Override
        public void close() {
            connectionClose.countDown();
        }
    };
    RealWebSocket webSocket = new RealWebSocket(fancyRequest, response.getWebSocketListener(), new SecureRandom());
    response.getWebSocketListener().onOpen(webSocket, fancyResponse);
    String name = "MockWebServer WebSocket " + request.getPath();
    webSocket.initReaderAndWriter(name, 0, streams);
    try {
        webSocket.loopReader();
        // Even if messages are no longer being read we need to wait for the connection close signal.
        try {
            connectionClose.await();
        } catch (InterruptedException ignored) {
        }
    } catch (IOException e) {
        webSocket.failWebSocket(e, null);
    } finally {
        closeQuietly(sink);
        closeQuietly(source);
    }
}
Also used : Response(okhttp3.Response) RealWebSocket(okhttp3.internal.ws.RealWebSocket) Request(okhttp3.Request) SecureRandom(java.security.SecureRandom) ByteString(okio.ByteString) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with RealWebSocket

use of okhttp3.internal.ws.RealWebSocket 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 3 with RealWebSocket

use of okhttp3.internal.ws.RealWebSocket 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 4 with RealWebSocket

use of okhttp3.internal.ws.RealWebSocket in project okhttp by square.

the class WebSocketHttpTest method clientCancelsIfCloseIsNotAcknowledged.

/** https://github.com/square/okhttp/issues/2788 */
@Test
public void clientCancelsIfCloseIsNotAcknowledged() throws Exception {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    RealWebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    // Initiate a close on the client, which will schedule a hard cancel in 500 ms.
    long closeAtNanos = System.nanoTime();
    webSocket.close(1000, "goodbye", 500);
    serverListener.assertClosing(1000, "goodbye");
    // Confirm that the hard cancel occurred after 500 ms.
    clientListener.assertFailure();
    long elapsedUntilFailure = System.nanoTime() - closeAtNanos;
    assertEquals(500, TimeUnit.NANOSECONDS.toMillis(elapsedUntilFailure), 250d);
    // Close the server and confirm it saw what we expected.
    server.close(1000, null);
    serverListener.assertClosed(1000, "goodbye");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Aggregations

SecureRandom (java.security.SecureRandom)2 Request (okhttp3.Request)2 RealWebSocket (okhttp3.internal.ws.RealWebSocket)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Response (okhttp3.Response)1 WebSocket (okhttp3.WebSocket)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 ByteString (okio.ByteString)1 Test (org.junit.Test)1