aboutsummaryrefslogtreecommitdiff
path: root/okhttp-ws
diff options
context:
space:
mode:
authorNeil Fuller <nfuller@google.com>2015-09-16 17:39:32 +0100
committerNeil Fuller <nfuller@google.com>2015-09-17 09:28:41 +0100
commit71b9f47b26fb57ac3e436a19519c6e3ec70e86eb (patch)
tree076fe135bc9d4bb0ae79ec94a5cba3649c18dd49 /okhttp-ws
parentbad0a11146d43955d3f3b949aa277f0dd7cc3abb (diff)
downloadokhttp-71b9f47b26fb57ac3e436a19519c6e3ec70e86eb.tar.gz
Pull latest code from upstream okhttp and okio
This change contains the OkHttp and Okio changes without modification. The only additions are the MODULE_LICENSE_APACHE2 files. This corresponds closely to OkHttp 2.5.0 and Okio 1.6.0. Behavior changes are documented in CHANGELOG.md. This change does not compile as is. The next commit makes the Android modifications required. okhttp: 4305dc3fabeab392eb56f2db51538e06c3a54e51 okio: 313436764bf35794e158c6171e319fee868298df Change-Id: I97ce07ff0472cdbce09f588863a1e5ccdcea0c20
Diffstat (limited to 'okhttp-ws')
-rw-r--r--okhttp-ws/pom.xml2
-rw-r--r--okhttp-ws/src/main/java/com/squareup/okhttp/internal/ws/RealWebSocket.java2
-rw-r--r--okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketCall.java30
-rw-r--r--okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketListener.java23
4 files changed, 25 insertions, 32 deletions
diff --git a/okhttp-ws/pom.xml b/okhttp-ws/pom.xml
index ae34464..81f8afd 100644
--- a/okhttp-ws/pom.xml
+++ b/okhttp-ws/pom.xml
@@ -6,7 +6,7 @@
<parent>
<groupId>com.squareup.okhttp</groupId>
<artifactId>parent</artifactId>
- <version>2.4.0-SNAPSHOT</version>
+ <version>2.6.0-SNAPSHOT</version>
</parent>
<artifactId>okhttp-ws</artifactId>
diff --git a/okhttp-ws/src/main/java/com/squareup/okhttp/internal/ws/RealWebSocket.java b/okhttp-ws/src/main/java/com/squareup/okhttp/internal/ws/RealWebSocket.java
index 07d763b..8d6b7c4 100644
--- a/okhttp-ws/src/main/java/com/squareup/okhttp/internal/ws/RealWebSocket.java
+++ b/okhttp-ws/src/main/java/com/squareup/okhttp/internal/ws/RealWebSocket.java
@@ -181,7 +181,7 @@ public abstract class RealWebSocket implements WebSocket {
} catch (IOException ignored) {
}
- listener.onFailure(e);
+ listener.onFailure(e, null);
}
/** Perform any tear-down work on the connection (close the socket, recycle, etc.). */
diff --git a/okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketCall.java b/okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketCall.java
index b499485..46ee8a1 100644
--- a/okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketCall.java
+++ b/okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketCall.java
@@ -22,7 +22,6 @@ import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.Internal;
-import com.squareup.okhttp.internal.NamedRunnable;
import com.squareup.okhttp.internal.Util;
import com.squareup.okhttp.internal.ws.RealWebSocket;
import com.squareup.okhttp.internal.ws.WebSocketProtocol;
@@ -61,19 +60,6 @@ public final class WebSocketCall {
if (!"GET".equals(request.method())) {
throw new IllegalArgumentException("Request must be GET: " + request.method());
}
- String url = request.urlString();
- String httpUrl;
- if (url.startsWith("ws://")) {
- httpUrl = "http://" + url.substring(5);
- } else if (url.startsWith("wss://")) {
- httpUrl = "https://" + url.substring(6);
- } else if (url.startsWith("http://") || url.startsWith("https://")) {
- httpUrl = url;
- } else {
- throw new IllegalArgumentException(
- "Request url must use 'ws', 'wss', 'http', or 'https' scheme: " + url);
- }
-
this.random = random;
byte[] nonce = new byte[16];
@@ -87,7 +73,6 @@ public final class WebSocketCall {
client.setProtocols(Collections.singletonList(com.squareup.okhttp.Protocol.HTTP_1_1));
request = request.newBuilder()
- .url(httpUrl)
.header("Upgrade", "websocket")
.header("Connection", "Upgrade")
.header("Sec-WebSocket-Key", key)
@@ -116,12 +101,12 @@ public final class WebSocketCall {
try {
createWebSocket(response, listener);
} catch (IOException e) {
- listener.onFailure(e);
+ listener.onFailure(e, response);
}
}
@Override public void onFailure(Request request, IOException e) {
- listener.onFailure(e);
+ listener.onFailure(e, null);
}
};
// TODO call.enqueue(responseCallback, true);
@@ -181,15 +166,10 @@ public final class WebSocketCall {
// TODO connection.setOwner(webSocket);
Internal.instance.connectionSetOwner(connection, webSocket);
- listener.onOpen(webSocket, request, response);
+ listener.onOpen(webSocket, response);
- // Start a dedicated thread for reading the web socket.
- new Thread(new NamedRunnable("OkHttp WebSocket reader %s", request.urlString()) {
- @Override protected void execute() {
- while (webSocket.readMessage()) {
- }
- }
- }).start();
+ while (webSocket.readMessage()) {
+ }
}
// Keep static so that the WebSocketCall instance can be garbage collected.
diff --git a/okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketListener.java b/okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketListener.java
index a113eed..8941b74 100644
--- a/okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketListener.java
+++ b/okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocketListener.java
@@ -15,7 +15,6 @@
*/
package com.squareup.okhttp.ws;
-import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import okio.Buffer;
@@ -25,7 +24,24 @@ import static com.squareup.okhttp.ws.WebSocket.PayloadType;
/** Listener for server-initiated messages on a connected {@link WebSocket}. */
public interface WebSocketListener {
- void onOpen(WebSocket webSocket, Request request, Response response) throws IOException;
+ /**
+ * Called when the request has successfully been upgraded to a web socket. This method is called
+ * on the message reading thread to allow setting up any state before the
+ * {@linkplain #onMessage message}, {@linkplain #onPong pong}, and {@link #onClose close}
+ * callbacks start.
+ * <p>
+ * <b>Do not</b> use this callback to write to the web socket. Start a new thread or use
+ * another thread in your application.
+ */
+ void onOpen(WebSocket webSocket, Response response);
+
+ /**
+ * Called when the transport or protocol layer of this web socket errors during communication.
+ *
+ * @param response Present when the failure is a direct result of the response (e.g., failed
+ * upgrade, non-101 response code, etc.). {@code null} otherwise.
+ */
+ void onFailure(IOException e, Response response);
/**
* Called when a server message is received. The {@code type} indicates whether the
@@ -53,7 +69,4 @@ public interface WebSocketListener {
* @param reason Reason for close or an empty string.
*/
void onClose(int code, String reason);
-
- /** Called when the transport or protocol layer of this web socket errors during communication. */
- void onFailure(IOException e);
}