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);
}
}
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");
}
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;
}
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");
}
Aggregations