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
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
gen/
bin
gen
.classpath
.project
local.properties
/.settings
project.properties
4 changes: 4 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
package="com.codebutler.android_websockets"
android:versionCode="1"
android:versionName="0.01">

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
</manifest>
119 changes: 111 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
# WebSocket client for Android
# THIS LIBRARY IS DEPRECATED IN FAVOR OF:

A very simple bare-minimum WebSocket client for Android.
[AndroidAsync](https://github.com/koush/AndroidAsync)








# WebSocket and Socket.IO client for Android

## Credits

The hybi parser is based on code from the [faye project](https://github.com/faye/faye-websocket-node). Faye is Copyright (c) 2009-2012 James Coglan. Many thanks for the great open-source library!

Ported from JavaScript to Java by [Eric Butler](https://twitter.com/codebutler) <eric@codebutler.com>.
The hybi parser was ported from JavaScript to Java by [Eric Butler](https://twitter.com/codebutler) <eric@codebutler.com>.

## Usage
The WebSocket client was written by [Eric Butler](https://twitter.com/codebutler) <eric@codebutler.com>.

Here's the entire API:
The Socket.IO client was written by [Koushik Dutta](https://twitter.com/koush).

The Socket.IO client component was ported from Koushik Dutta's AndroidAsync(https://github.com/koush/AndroidAsync) by [Vinay S Shenoy](https://twitter.com/vinaysshenoy)

## WebSocket Usage

```java
List<BasicNameValuePair> extraHeaders = Arrays.asList(
new BasicNameValuePair("Cookie", "session=abcd");
new BasicNameValuePair("Cookie", "session=abcd")
);

WebSocketClient client = new WebSocketClient(URI.create("wss://irccloud.com"), new WebSocketClient.Handler() {
WebSocketClient client = new WebSocketClient(URI.create("wss://irccloud.com"), new WebSocketClient.Listener() {
@Override
public void onConnect() {
Log.d(TAG, "Connected!");
Expand All @@ -30,7 +43,7 @@ WebSocketClient client = new WebSocketClient(URI.create("wss://irccloud.com"), n

@Override
public void onMessage(byte[] data) {
Log.d(TAG, String.format("Got binary message! %s", toHexString(data));
Log.d(TAG, String.format("Got binary message! %s", toHexString(data)));
}

@Override
Expand All @@ -42,6 +55,7 @@ WebSocketClient client = new WebSocketClient(URI.create("wss://irccloud.com"), n
public void onError(Exception error) {
Log.e(TAG, "Error!", error);
}

}, extraHeaders);

client.connect();
Expand All @@ -52,6 +66,94 @@ client.send(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF });
client.disconnect();
```

## Socket.IO Usage

```java
SocketIOClient.connect("http://localhost:80", new ConnectCallback() {

@Override
public void onConnectCompleted(Exception ex, SocketIOClient client) {

if (ex != null) {
return;
}

//Save the returned SocketIOClient instance into a variable so you can disconnect it later
client.setDisconnectCallback(MainActivity.this);
client.setErrorCallback(MainActivity.this);
client.setJSONCallback(MainActivity.this);
client.setStringCallback(MainActivity.this);

//You need to explicitly specify which events you are interested in receiving
client.addListener("news", MainActivity.this);

client.of("/chat", new ConnectCallback() {

@Override
public void onConnectCompleted(Exception ex, SocketIOClient client) {

if (ex != null) {
ex.printStackTrace();
return;
}

//This client instance will be using the same websocket as the original client,
//but will point to the indicated endpoint
client.setDisconnectCallback(MainActivity.this);
client.setErrorCallback(MainActivity.this);
client.setJSONCallback(MainActivity.this);
client.setStringCallback(MainActivity.this);
client.addListener("a message", MainActivity.this);

}
});

}
}, new Handler());


@Override
public void onEvent(String event, JSONArray argument, Acknowledge acknowledge) {
try {
Log.d("MainActivity", "Event:" + event + "Arguments:"
+ argument.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}

}

@Override
public void onString(String string, Acknowledge acknowledge) {
Log.d("MainActivity", string);

}

@Override
public void onJSON(JSONObject json, Acknowledge acknowledge) {
try {
Log.d("MainActivity", "json:" + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}

}

@Override
public void onError(String error) {
Log.d("MainActivity", error);

}

@Override
public void onDisconnect(Exception e) {
Log.d(mComponentTag, "Disconnected:" + e.getMessage());

}

```



## TODO

Expand All @@ -64,6 +166,7 @@ client.disconnect();

Copyright (c) 2009-2012 James Coglan
Copyright (c) 2012 Eric Butler
Copyright (c) 2012 Koushik Dutta

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the 'Software'), to deal in
Expand Down
13 changes: 11 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="android_websockets" default="help">
<project name="android-websockets" default="help">

<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
Expand Down Expand Up @@ -28,6 +28,15 @@
-->
<property file="ant.properties" />

<!-- if sdk.dir was not set from one of the property file, then
get it from the ANDROID_HOME env var.
This must be done before we load project.properties since
the proguard config can use sdk.dir -->
<property environment="env" />
<condition property="sdk.dir" value="${env.ANDROID_HOME}">
<isset property="env.ANDROID_HOME" />
</condition>

<!-- The project.properties file is created and updated by the 'android'
tool, as well as ADT.

Expand All @@ -41,7 +50,7 @@

<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
unless="sdk.dir"
/>

Expand Down
20 changes: 20 additions & 0 deletions proguard-project.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
2 changes: 1 addition & 1 deletion project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

android.library=true
# Project target.
target=android-14
target=android-8
37 changes: 35 additions & 2 deletions src/com/codebutler/android_websockets/HybiParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void parseOpcode(byte data) throws ProtocolError {
throw new ProtocolError("Bad opcode");
}

if (FRAGMENTED_OPCODES.contains(mOpcode) && !mFinal) {
if (!FRAGMENTED_OPCODES.contains(mOpcode) && !mFinal) {
throw new ProtocolError("Expected non-final packet");
}

Expand Down Expand Up @@ -332,9 +332,42 @@ private int getInteger(byte[] bytes) throws ProtocolError {
}
return (int) i;
}

/**
* Copied from AOSP Arrays.java.
*/
/**
* Copies elements from {@code original} into a new array, from indexes start (inclusive) to
* end (exclusive). The original order of elements is preserved.
* If {@code end} is greater than {@code original.length}, the result is padded
* with the value {@code (byte) 0}.
*
* @param original the original array
* @param start the start index, inclusive
* @param end the end index, exclusive
* @return the new array
* @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length}
* @throws IllegalArgumentException if {@code start > end}
* @throws NullPointerException if {@code original == null}
* @since 1.6
*/
private static byte[] copyOfRange(byte[] original, int start, int end) {
if (start > end) {
throw new IllegalArgumentException();
}
int originalLength = original.length;
if (start < 0 || start > originalLength) {
throw new ArrayIndexOutOfBoundsException();
}
int resultLength = end - start;
int copyLength = Math.min(resultLength, originalLength - start);
byte[] result = new byte[resultLength];
System.arraycopy(original, start, result, 0, copyLength);
return result;
}

private byte[] slice(byte[] array, int start) {
return Arrays.copyOfRange(array, start, array.length);
return copyOfRange(array, start, array.length);
}

public static class ProtocolError extends IOException {
Expand Down
Loading