Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 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
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
gen/
bin
gen
.classpath
.project
local.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>
66 changes: 61 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# WebSocket client for Android
# WebSocket and Socket.IO client for Android

A very simple bare-minimum WebSocket client for Android.
A very simple bare-minimum 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).

## WebSocket Usage

```java
List<BasicNameValuePair> extraHeaders = Arrays.asList(
Expand Down Expand Up @@ -52,6 +54,59 @@ client.send(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF });
client.disconnect();
```

## Socket.IO Usage

```java
SocketIOClient client = new SocketIOClient(URI.create("wss://example.com"), new SocketIOClient.Handler() {
@Override
public void onConnect() {
Log.d(TAG, "Connected!");
}

@Override
public void on(String event, JSONArray arguments) {
Log.d(TAG, String.format("Got event %s: %s", event, arguments.toString()));
}

@Override
public void onJSON(JSONObject json) {
try {
Log.d(TAG, String.format("Got JSON Object: %s", json.toString()));
} catch(JSONException e) {
}
}

@Override
public void onMessage(String message) {
Log.d(TAG, String.format("Got message: %s", message));
}

@Override
public void onDisconnect(int code, String reason) {
Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason));
}

@Override
public void onError(Exception error) {
Log.e(TAG, "Error!", error);
}
});

client.connect();

// Later…
client.emit("Message"); //Message
JSONArray arguments = new JSONArray();
arguments.put("first argument");
JSONObject second = new JSONObject();
second.put("dictionary", true);
client.emit(second); //JSON Message
arguments.put(second);
client.emit("hello", arguments); //Event
client.disconnect();
```



## TODO

Expand All @@ -64,6 +119,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
39 changes: 36 additions & 3 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;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backported this to Froyo, was missing this function.

/**
* 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 Expand Up @@ -380,4 +413,4 @@ public byte[] readBytes(int length) throws IOException {
return buffer;
}
}
}
}
Loading