add request header, cancel WebSocketCall

This commit is contained in:
Yu-Hsuan Lin
2015-04-13 04:01:04 +08:00
parent b3586bb0b1
commit 3ccc42ab2c

View File

@@ -32,6 +32,7 @@ public class WebSocket extends Transport {
public static final String NAME = "websocket"; public static final String NAME = "websocket";
private com.squareup.okhttp.ws.WebSocket ws; private com.squareup.okhttp.ws.WebSocket ws;
private WebSocketCall wsCall;
public WebSocket(Options opts) { public WebSocket(Options opts) {
super(opts); super(opts);
@@ -52,10 +53,12 @@ public class WebSocket extends Transport {
SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
client.setSslSocketFactory(factory); client.setSslSocketFactory(factory);
} }
final Request request = new Request.Builder() Request.Builder builder = new Request.Builder().url(uri());
.url(uri()) for (Map.Entry<String, String> entry : headers.entrySet()) {
.build(); builder.addHeader(entry.getKey(), entry.getValue());
WebSocketCall.create(client, request).enqueue(new WebSocketListener() { }
final Request request = builder.build();
(wsCall = WebSocketCall.create(client, request)).enqueue(new WebSocketListener() {
@Override @Override
public void onOpen(com.squareup.okhttp.ws.WebSocket webSocket, Request request, Response response) throws IOException { public void onOpen(com.squareup.okhttp.ws.WebSocket webSocket, Request request, Response response) throws IOException {
ws = webSocket; ws = webSocket;
@@ -175,12 +178,17 @@ public class WebSocket extends Transport {
} }
protected void doClose() { protected void doClose() {
if (this.ws != null) { if (wsCall != null) {
wsCall.cancel();
wsCall = null;
}
if (ws != null) {
try { try {
this.ws.close(1000, ""); ws.close(1000, "");
} catch (IOException e) { } catch (IOException e) {
onError("doClose error", e); onError("doClose error", e);
} }
ws = null;
} }
} }