Skip to content

Commit

Permalink
Add convenience methods to WebSocketCall for symmetry with Call.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Sep 20, 2016
1 parent 765ffb3 commit 5ae1a9d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
27 changes: 23 additions & 4 deletions okhttp/src/main/java/okhttp3/RealWebSocketCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
final class RealWebSocketCall implements WebSocketCall {
private static final List<Protocol> ONLY_HTTP1 = Collections.singletonList(Protocol.HTTP_1_1);

/** The application's original request unadulterated by web socket headers. */
private final Request originalRequest;
private final RealCall call;
private final Random random;
private final String key;
Expand All @@ -59,6 +61,7 @@ final class RealWebSocketCall implements WebSocketCall {
.protocols(ONLY_HTTP1)
.build();

originalRequest = request;
request = request.newBuilder()
.header("Upgrade", "websocket")
.header("Connection", "Upgrade")
Expand Down Expand Up @@ -90,10 +93,6 @@ final class RealWebSocketCall implements WebSocketCall {
call.enqueue(responseCallback);
}

@Override public void cancel() {
call.cancel();
}

StreamWebSocket create(Response response, WebSocketListener listener) throws IOException {
if (response.code() != 101) {
throw new ProtocolException("Expected HTTP 101 response but was '"
Expand Down Expand Up @@ -134,6 +133,26 @@ StreamWebSocket create(Response response, WebSocketListener listener) throws IOE
return new StreamWebSocket(streamAllocation, random, replyExecutor, listener, response, name);
}

@Override public Request request() {
return originalRequest;
}

@Override public void cancel() {
call.cancel();
}

@Override public boolean isExecuted() {
return call.isExecuted();
}

@Override public boolean isCanceled() {
return call.isCanceled();
}

@Override public WebSocketCall clone() {
return new RealWebSocketCall(call.client, originalRequest, random);
}

// Keep static so that the WebSocketCall instance can be garbage collected.
static final class StreamWebSocket extends RealWebSocket {
private final StreamAllocation streamAllocation;
Expand Down
19 changes: 18 additions & 1 deletion okhttp/src/main/java/okhttp3/WebSocketCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package okhttp3;

public interface WebSocketCall {
public interface WebSocketCall extends Cloneable {
/** Returns the original request that initiated this call. */
Request request();

/**
* Schedules the request to be executed at some point in the future.
*
Expand All @@ -33,6 +36,20 @@ public interface WebSocketCall {
/** Cancels the request, if possible. Requests that are already complete cannot be canceled. */
void cancel();

/**
* Returns true if this call has been {@linkplain #enqueue(WebSocketListener) enqueued}. It is an
* error to enqueue a call more than once.
*/
boolean isExecuted();

boolean isCanceled();

/**
* Create a new, identical call to this one which can be enqueued even if this call has already
* been.
*/
WebSocketCall clone();

interface Factory {
WebSocketCall newWebSocketCall(Request request);
}
Expand Down

0 comments on commit 5ae1a9d

Please sign in to comment.