Search in sources :

Example 51 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketRecorder method onMessage.

@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
    Platform.get().log("[WS " + name + "] onMessage", Platform.INFO, 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 52 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketRecorder method onMessage.

@Override
public void onMessage(WebSocket webSocket, String text) {
    Platform.get().log("[WS " + name + "] onMessage", Platform.INFO, null);
    WebSocketListener delegate = this.delegate;
    if (delegate != null) {
        this.delegate = null;
        delegate.onMessage(webSocket, text);
    } else {
        Message event = new Message(text);
        events.add(event);
    }
}
Also used : WebSocketListener(okhttp3.WebSocketListener)

Example 53 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method successfulExtensions.

private void successfulExtensions(String extensionsHeader) throws Exception {
    webServer.enqueue(new MockResponse().addHeader("Sec-WebSocket-Extensions", extensionsHeader).withWebSocketUpgrade(serverListener));
    WebSocket client = newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    // Server to client message big enough to be compressed.
    String message1 = TestUtil.repeat('a', (int) RealWebSocket.DEFAULT_MINIMUM_DEFLATE_SIZE);
    server.send(message1);
    clientListener.assertTextMessage(message1);
    // Client to server message big enough to be compressed.
    String message2 = TestUtil.repeat('b', (int) RealWebSocket.DEFAULT_MINIMUM_DEFLATE_SIZE);
    client.send(message2);
    serverListener.assertTextMessage(message2);
    // Empty server to client message.
    String message3 = "";
    server.send(message3);
    clientListener.assertTextMessage(message3);
    // Empty client to server message.
    String message4 = "";
    client.send(message4);
    serverListener.assertTextMessage(message4);
    // Server to client message that shares context with message1.
    String message5 = message1 + message1;
    server.send(message5);
    clientListener.assertTextMessage(message5);
    // Client to server message that shares context with message2.
    String message6 = message2 + message2;
    client.send(message6);
    serverListener.assertTextMessage(message6);
    closeWebSockets(client, server);
    RecordedRequest upgradeRequest = webServer.takeRequest();
    assertThat(upgradeRequest.getHeader("Sec-WebSocket-Extensions")).isEqualTo("permessage-deflate");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket)

Example 54 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method closeReasonMaximumLength.

@Test
public void closeReasonMaximumLength() {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    String clientReason = repeat('C', 123);
    String serverReason = repeat('S', 123);
    WebSocket webSocket = newWebSocket();
    WebSocket server = serverListener.assertOpen();
    clientListener.assertOpen();
    webSocket.close(1000, clientReason);
    serverListener.assertClosing(1000, clientReason);
    server.close(1000, serverReason);
    clientListener.assertClosing(1000, serverReason);
    clientListener.assertClosed(1000, serverReason);
    serverListener.assertClosed(1000, clientReason);
}
Also used : MockResponse(mockwebserver3.MockResponse) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 55 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method throwingOnMessageClosesImmediatelyAndFails.

@Test
public void throwingOnMessageClosesImmediatelyAndFails() {
    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.assertFailure(EOFException.class);
    serverListener.assertExhausted();
}
Also used : MockResponse(mockwebserver3.MockResponse) WebSocketListener(okhttp3.WebSocketListener) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

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