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

WebSocket Error when send(String) or send(byte) #34

Open
wants to merge 86 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
7b20146
Add initial Socket.IO support.
koush May 4, 2012
47a7822
Add proper cleanup for errors.
koush May 4, 2012
3e4a159
Fix cleanup to null out the client. Prevent multiple connections whil…
koush May 4, 2012
2c88ddb
Documentation for Socket.IO support.
koush May 4, 2012
76d8cf5
Remove extraHeaders for Socket.IO connections.
koush May 4, 2012
b0c10cc
Copyright for my changes.
koush May 4, 2012
44caf8e
Update the title and credits.
koush May 4, 2012
c511e73
Clean up the titles around usage.
koush May 4, 2012
6558e4c
Update SocketIOClient to actually use the SocketIOClient class.
koush May 4, 2012
4f49431
Rename send to emit, similar to how the node/javascript code names it.
koush May 4, 2012
8bce20f
Fixed bug with incorrect URL when base URI with trailing slash used.
FilipZawada May 17, 2012
71be505
Update README.md
mike-stewart Nov 29, 2012
e088b9e
Merge pull request #2 from mike-stewart/patch-1
koush Nov 29, 2012
80322b3
Fire onConnect event when connection established
bkase Jan 2, 2013
6adfa2a
Fixed crash on a no-argument message
bkase Jan 2, 2013
7de2163
Merge pull request #4 from bkase/fix_noargs
koush Jan 3, 2013
9760910
Merge pull request #1 from FilipZawada/master
koush Jan 3, 2013
620cc90
Merge pull request #3 from bkase/fire_connect
koush Jan 3, 2013
b45e80c
Update src/com/codebutler/android_websockets/HybiParser.java
koush Jan 3, 2013
18a3624
Merge pull request #5 from codebutler/master
koush Jan 3, 2013
e33c05b
Added heartbeat replies, fixed two bugs, and added logging.
mike-stewart Jan 10, 2013
8c8bf39
Merge pull request #6 from mike-stewart/master
koush Jan 10, 2013
86617e5
Create an SSL socket for https connections.
mike-stewart Feb 5, 2013
e683f20
Merge pull request #7 from mike-stewart/master
koush Feb 5, 2013
4b2fa51
Added manifest target sdk
Mar 1, 2013
fbd83b5
Added support for JSONMessage and Message to SocketIOClient
Mar 1, 2013
acc3552
Renamed method onJSONMessage to onJSON
vinaysshenoy Mar 2, 2013
85e5926
Merge pull request #9 from vinaysshenoy/master
koush Mar 2, 2013
f440715
Update README.md
vinaysshenoy Mar 2, 2013
fc5d023
Update README.md
vinaysshenoy Mar 2, 2013
d8645d6
Merge pull request #10 from vinaysshenoy/master
koush Mar 2, 2013
a6a3b99
update project properties and build xml
koush Mar 6, 2013
3cbe3ee
Fixed double firing of onConnect event in SocketIOClient handler.
mike-stewart Mar 9, 2013
878842d
Merge pull request #12 from mike-stewart/master
koush Mar 9, 2013
1174d7e
add isConnected method
pefoley2 Apr 2, 2013
84119d1
Merge pull request #13 from pefoley2/master
koush Apr 8, 2013
dad5c6f
Update SocketIOClient.java
orenda May 8, 2013
c62edc2
Fix syntax errors in README.md's example code
cpeterso May 9, 2013
ecaf18b
Merge pull request #15 from cpeterso/cpeterso/fix-readme-errors
koush May 9, 2013
e1877d6
Added support for Endpoints
May 20, 2013
b7e9b76
Added listener event for connecting to endpoints
May 20, 2013
96ae660
Merge pull request #16 from vinaysshenoy/master
koush May 20, 2013
5408843
Merge pull request #14 from orenda/patch-1
koush May 20, 2013
4c28f20
Added support for Acknowledges
May 20, 2013
2f586e0
Update README.md
vinaysshenoy May 20, 2013
f7487f2
add end pointed connected override to the handler
liorsion May 21, 2013
3a2f9d9
only socketio client has endpoints, normal websocket doesn't
liorsion May 21, 2013
7555935
Merge pull request #18 from liorsion/master
koush May 21, 2013
604cfdd
Update README.md
vinaysshenoy May 22, 2013
21ef8ee
Added support for Acknowledges
May 22, 2013
f947253
Merge branch 'master' of
May 22, 2013
36cc209
Avoid mSocket NPE if WebSocketClient.disconnect() is called twice
cpeterso May 23, 2013
ef5f352
Merge pull request #19 from cpeterso/cpeterso/handle-double-disconnect
koush May 24, 2013
c7f22d4
Fixed merge conflicts
May 24, 2013
8dfb26d
Merge pull request #17 from vinaysshenoy/master
koush May 30, 2013
57be2c6
Avoid calling cleanup twice on error condition
ElMassimo Jun 1, 2013
7ea59a8
Merge pull request #20 from ElMassimo/cleanup-called-twice
koush Jun 1, 2013
61be4e5
Fixed msgId in emit() messages being appended with '+' when Ackowledge
Jun 6, 2013
98ec596
Merge pull request #21 from vinaysshenoy/master
koush Jun 6, 2013
7583bf9
Implemented auto-reconnect with exponential backoff for SocketIOClient
Jun 15, 2013
fb3865c
Changed cleanup code because we can reconnect on error
Jun 15, 2013
c3d08a1
Fixed formatting
Jun 15, 2013
17dbf03
Add support of message endPoint for types : message, message JSON and
Jul 1, 2013
2a93904
Merge pull request #22 from vinaysshenoy/master
koush Jul 1, 2013
5d3c4b7
Merge branch 'master' of git://github.com/yapiti/android-websockets
koush Jul 5, 2013
386dfed
Changed endpoints to be optional when emitting messages
Jul 5, 2013
475887a
Merge pull request #24 from vinaysshenoy/master
koush Jul 5, 2013
ad5a9f3
Ported AndroidAsync SocketIO framework
Jul 9, 2013
92911b8
Removed eventcallback addition to SocketIOClient as we need to use
Jul 9, 2013
e706535
Added wrappers around Callbacks to mimic earlier Handler interface
Jul 11, 2013
58c1f5e
Removed Handler interface, Invoking callbacks on the passed Handler
Jul 11, 2013
e61b05e
Repackaged HybiParser and WebSocketClient
Jul 11, 2013
7215aea
Switched back to original WebsocketClient and HybiParser
Jul 12, 2013
0e0a95f
Merge pull request #26 from vinaysshenoy/v2
koush Jul 12, 2013
b4ad055
Merge pull request #1 from vinaysshenoy/v2
vinaysshenoy Jul 12, 2013
29e0042
Update README.md
vinaysshenoy Jul 12, 2013
383d075
Update README.md
vinaysshenoy Jul 12, 2013
9bdd3e7
Merge pull request #27 from vinaysshenoy/master
koush Jul 12, 2013
9e5b14b
Verify server's Sec-WebSocket-Accept
payden Oct 14, 2013
052d141
Merge pull request #28 from payden/verify-secret
koush Oct 14, 2013
3f5788a
Better handle queries when building the session url
Jan 30, 2014
c350b24
Merge pull request #30 from rahulparsani/master
koush Jan 30, 2014
75e052b
Added support for headers in SocketIORequest
sergiocampama Apr 11, 2014
460bd2c
Merge pull request #33 from sergiocampama/headers
koush Apr 11, 2014
0f1a2fa
Update README.md
koush Apr 11, 2014
d183761
Update README.md
koush Apr 11, 2014
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -3,3 +3,4 @@ gen
.classpath
.project
local.properties
/.settings
125 changes: 99 additions & 26 deletions src/com/codebutler/android_websockets/SocketIOClient.java
Original file line number Diff line number Diff line change
@@ -18,12 +18,15 @@

import android.net.http.AndroidHttpClient;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

public class SocketIOClient {
public static interface Handler {
public void onConnect();

public void onConnectToEndpoint(String endpoint);

public void on(String event, JSONArray arguments);

public void onDisconnect(int code, String reason);
@@ -36,16 +39,28 @@ public static interface Handler {
}

private static final String TAG = "SocketIOClient";

String mURL;
Handler mHandler;
String mSession;
int mHeartbeat;
WebSocketClient mClient;
String mEndpoint;

public SocketIOClient(URI uri, Handler handler) {
// remove trailing "/" from URI, in case user provided e.g. http://test.com/
mURL = uri.toString().replaceAll("/$", "") + "/socket.io/1/";
this(uri, handler, null);
}

public SocketIOClient(URI uri, Handler handler, String namespace) {
mEndpoint = namespace;

if (TextUtils.isEmpty(namespace)) {
mEndpoint = "socket.io";
}

// remove trailing "/" from URI, in case user provided e.g.
// http://test.com/
mURL = uri.toString().replaceAll("/$", "") + "/" + mEndpoint + "/1/";
mHandler = handler;
}

@@ -54,8 +69,7 @@ private static String downloadUriAsString(final HttpUriRequest req) throws IOExc
try {
HttpResponse res = client.execute(req);
return readToEnd(res.getEntity().getContent());
}
finally {
} finally {
client.close();
}
}
@@ -91,21 +105,21 @@ public void run() {
}
});
}

public void emit(final String message) {
mSendHandler.post(new Runnable() {

@Override
public void run() {
mClient.send(String.format("3:::%s", message));
}
});
}

public void emit(final JSONObject jsonMessage) {

mSendHandler.post(new Runnable() {

@Override
public void run() {
mClient.send(String.format("4:::%s", jsonMessage.toString()));
@@ -130,7 +144,11 @@ public void onMessage(String message) {
switch (code) {
case 1:
// connect
mHandler.onConnect();
if (!TextUtils.isEmpty(parts[2])) {
mHandler.onConnectToEndpoint(parts[2]);
} else {
mHandler.onConnect();
}
break;
case 2:
// heartbeat
@@ -140,10 +158,10 @@ public void onMessage(String message) {
// message
final String messageId = parts[1];
final String dataString = parts[3];
if(!"".equals(messageId)) {

if (!"".equals(messageId)) {
mSendHandler.post(new Runnable() {

@Override
public void run() {
mClient.send(String.format("6:::%s", messageId));
@@ -154,20 +172,20 @@ public void run() {
break;
}
case 4: {
//json message
// json message
final String messageId = parts[1];
final String dataString = parts[3];

JSONObject jsonMessage = null;

try {
jsonMessage = new JSONObject(dataString);
} catch(JSONException e) {
} catch (JSONException e) {
jsonMessage = new JSONObject();
}
if(!"".equals(messageId)) {
if (!"".equals(messageId)) {
mSendHandler.post(new Runnable() {

@Override
public void run() {
mClient.send(String.format("6:::%s", messageId));
@@ -211,8 +229,7 @@ public void run() {
default:
throw new Exception("unknown code");
}
}
catch (Exception ex) {
} catch (Exception ex) {
cleanup();
onError(ex);
}
@@ -252,7 +269,7 @@ public void disconnect() throws IOException {
private void cleanup() {
mClient.disconnect();
mClient = null;

mSendLooper.quit();
mSendLooper = null;
mSendHandler = null;
@@ -284,12 +301,68 @@ public void run() {
connectSession();

Looper.loop();
}
catch (Exception e) {
} catch (Exception e) {
mHandler.onError(e);
}
};
}.start();
}
}

/**
* Connect to an endpoint
*/
public void connectToEndpoint(final String endpoint) {

if (mClient.isConnected() && !TextUtils.isEmpty(endpoint)) {
mEndpoint = endpoint;
mSendHandler.post(new Runnable() {

@Override
public void run() {
mClient.send("1::" + endpoint);
}
});
}
}

/**
* Disconnect from an endpoint or socket
*
* @param endpoint
* {@code null} to disconnect the entire socket, otherwise the
* endpoint to disconnect from
*/
public void sendDisconnect(final String endpoint) {

if (TextUtils.isEmpty(endpoint)) {

mSendHandler.post(new Runnable() {

@Override
public void run() {
mClient.send("0");
}
});
}

else {
mSendHandler.post(new Runnable() {

@Override
public void run() {
mClient.send("0::" + endpoint);
}
});
}
}

/**
* Get the current connected endpoint
*
* @return The current connected endpoint, "socket.io" if connected to the
* default endpoint
*/
public String getConnectedEndpoint() {
return mEndpoint;
}
}