Skip to content

Commit 7454f31

Browse files
Add Android wrapper
1 parent 8cae421 commit 7454f31

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

.classpath

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="src" path="test"/>
5+
<classpathentry kind="src" path="android"/>
56
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
67
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
8+
<classpathentry kind="var" path="ANDROID_PLATFORM/android.jar"/>
79
<classpathentry kind="output" path="bin"/>
810
</classpath>

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
bin
22

3+
/local.properties
4+
/build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package net.rcode.wsclient;
2+
3+
import android.os.Handler;
4+
5+
/**
6+
* Android specific WebSocket client that interfaces with the Looper
7+
* to provide threading control.
8+
*
9+
* @author stella
10+
*
11+
*/
12+
public class AndroidWebSocket extends WebSocket {
13+
private Handler handler;
14+
15+
/**
16+
* Instantiate the class so that events are directed back at the Looper
17+
* attached to this thread. In typical parlance, this means that you must
18+
* instantiate the instance on the main thread. A task will be posted to
19+
* the current thread to start the instance.
20+
*
21+
* @param url
22+
* @param requestedProtocols
23+
*/
24+
public AndroidWebSocket(String url, String... requestedProtocols) {
25+
this(new Handler(), url, requestedProtocols);
26+
handler.post(new Runnable() {
27+
@Override
28+
public void run() {
29+
start();
30+
}
31+
});
32+
}
33+
34+
/**
35+
* Instantiate the instance targeting an explicit handler. This variant is
36+
* typically not used. This constructor does not automatically post a message
37+
* to the handler to start the instance. The caller must arrange to start
38+
* it via another means.
39+
*
40+
* @param url
41+
* @param requestedProtocols
42+
*/
43+
public AndroidWebSocket(Handler handler, String url, String... requestedProtocols) {
44+
super(url, requestedProtocols);
45+
handler=new Handler();
46+
}
47+
48+
@Override
49+
protected void dispatchEvent(final Event event, final EventListener l) {
50+
handler.post(new Runnable() {
51+
@Override
52+
public void run() {
53+
l.handleEvent(event);
54+
}
55+
});
56+
}
57+
}

build.xml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<project name="java-websocket-client" default="dist">
2+
<property file="local.properties"/>
3+
4+
<target name="init-base">
5+
<mkdir dir="build"/>
6+
<mkdir dir="build/classes"/>
7+
<mkdir dir="build/android-classes"/>
8+
<mkdir dir="build/dist"/>
9+
<available property="has.android.jar" file="${android.jar}"/>
10+
</target>
11+
12+
<target name="init-no-android" unless="has.android.jar">
13+
<echo>Android support will not be built because the property android.jar is not</echo>
14+
<echo>set correctly. It is currently ${android.jar}</echo>
15+
</target>
16+
17+
<target name="init" depends="init-base,init-no-android"/>
18+
19+
<target name="clean">
20+
<delete dir="build"/>
21+
</target>
22+
23+
<target name="compile-core">
24+
<javac srcdir="src" destdir="build/classes" target="1.5" source="1.5" debug="true" includeantruntime="false"/>
25+
</target>
26+
27+
<target name="compile-android" if="has.android.jar">
28+
<javac srcdir="android" destdir="build/android-classes" target="1.5" source="1.5" debug="true" includeantruntime="false">
29+
<classpath>
30+
<pathelement location="build/classes"/>
31+
<pathelement location="${android.jar}"/>
32+
</classpath>
33+
</javac>
34+
</target>
35+
36+
<target name="compile" depends="init,compile-core,compile-android"/>
37+
38+
<target name="dist-android" if="has.android.jar">
39+
<jar jarfile="build/dist/java-websocket-client-android.jar">
40+
<fileset dir="build/classes"/>
41+
<fileset dir="build/android-classes"/>
42+
</jar>
43+
</target>
44+
45+
<target name="dist" depends="compile,dist-android">
46+
<jar jarfile="build/dist/java-websocket-client.jar" basedir="build/classes"/>
47+
</target>
48+
</project>
49+

src/net/rcode/wsclient/WebSocket.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,8 @@ public synchronized void removeAllListeners() {
353353
* does nothing. Otherwise, starts the processing thread. Environment specific
354354
* subclasses will typically integrate with the native message loop to handle this
355355
* detail for you.
356-
* @throws InterruptedException
357356
*/
358-
public void start() throws InterruptedException {
357+
public void start() {
359358
if (started) return;
360359
started=true;
361360
readerThread=new Thread("WebSocket read " + url) {

0 commit comments

Comments
 (0)