Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added time out to websocketclient #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
gen/
#Android generated
bin
gen
.project
.classpath
.settings

#Command line
local.properties
8 changes: 6 additions & 2 deletions src/com/codebutler/android_websockets/HybiParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,17 @@ private byte[] frame(Object data, int opcode, int errorCode) {
return frame;
}

public void ping() {
mClient.sendFrame(frame(new byte[]{}, OP_PING, -1));
}

public void ping(String message) {
mClient.send(frame(message, OP_PING, -1));
mClient.sendFrame(frame(message, OP_PING, -1));
}

public void close(int code, String reason) {
if (mClosed) return;
mClient.send(frame(reason, OP_CLOSE, code));
mClient.sendFrame(frame(reason, OP_CLOSE, code));
mClosed = true;
}

Expand Down
62 changes: 45 additions & 17 deletions src/com/codebutler/android_websockets/WebSocketClient.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package com.codebutler.android_websockets;

import android.os.Handler;
import android.os.HandlerThread;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import org.apache.http.*;
import org.apache.http.client.HttpResponseException;
import org.apache.http.message.BasicLineParser;
import org.apache.http.message.BasicNameValuePair;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpResponseException;
import org.apache.http.message.BasicLineParser;
import org.apache.http.message.BasicNameValuePair;

import android.os.Handler;
import android.os.HandlerThread;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

public class WebSocketClient {
private static final String TAG = "WebSocketClient";

Expand All @@ -35,6 +42,7 @@ public class WebSocketClient {
private HandlerThread mHandlerThread;
private Handler mHandler;
private List<BasicNameValuePair> mExtraHeaders;
private int mTimeout;
private HybiParser mParser;

private final Object mSendLock = new Object();
Expand All @@ -46,9 +54,14 @@ public static void setTrustManagers(TrustManager[] tm) {
}

public WebSocketClient(URI uri, Listener listener, List<BasicNameValuePair> extraHeaders) {
this(uri, listener, extraHeaders, -1);
}

public WebSocketClient(URI uri, Listener listener, List<BasicNameValuePair> extraHeaders, int timeout) {
mURI = uri;
mListener = listener;
mExtraHeaders = extraHeaders;
mTimeout = timeout;
mParser = new HybiParser(this);

mHandlerThread = new HandlerThread("websocket-thread");
Expand Down Expand Up @@ -80,8 +93,15 @@ public void run() {
URI origin = new URI(originScheme, "//" + mURI.getHost(), null);

SocketFactory factory = mURI.getScheme().equals("wss") ? getSSLSocketFactory() : SocketFactory.getDefault();
mSocket = factory.createSocket(mURI.getHost(), port);

mSocket = factory.createSocket();

InetSocketAddress socketAddress = new InetSocketAddress(mURI.getHost(), port);
if(mTimeout != -1) {
mSocket.connect(socketAddress, mTimeout);
} else {
mSocket.connect(socketAddress);
}

PrintWriter out = new PrintWriter(mSocket.getOutputStream());
out.print("GET " + path + " HTTP/1.1\r\n");
out.print("Upgrade: websocket\r\n");
Expand Down Expand Up @@ -164,6 +184,14 @@ public void send(byte[] data) {
sendFrame(mParser.frame(data));
}

public void ping() {
mParser.ping();
}

public void ping(String message) {
mParser.ping(message);
}

private StatusLine parseStatusLine(String line) {
if (TextUtils.isEmpty(line)) {
return null;
Expand Down