Skip to content

Commit 0a7a04c

Browse files
committed
init doc
0 parents  commit 0a7a04c

11 files changed

Lines changed: 312 additions & 0 deletions

File tree

websockets/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

websockets/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**Android webSocket client for Ratchet Server** http://socketo.me/
2+
This android library use autobahn-java https://github.com/crossbario/autobahn-java
3+
4+
Installation

websockets/build.gradle

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
6+
7+
defaultConfig {
8+
minSdkVersion 14
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
15+
}
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
}
23+
24+
dependencies {
25+
compile fileTree(include: ['*.jar'], dir: 'libs')
26+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27+
exclude group: 'com.android.support', module: 'support-annotations'
28+
})
29+
testCompile 'junit:junit:4.12'
30+
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
31+
compile 'com.google.code.gson:gson:2.8.1'
32+
}

websockets/libs/autobahn-0.5.0.jar

52.4 KB
Binary file not shown.

websockets/proguard-rules.pro

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/geecko/Environnement/Android/sdk_2/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.craftsman.websockets;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.craftsman.websockets.test", appContext.getPackageName());
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
3+
package="com.craftsman.websockets">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application android:allowBackup="true" android:label="@string/app_name"
8+
android:supportsRtl="true">
9+
10+
</application>
11+
12+
</manifest>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.craftsman.websockets;
2+
3+
/**
4+
* Created by ALI SHADAÏ (Software Craftman) on 15/09/2017.
5+
*/
6+
7+
public interface Ws {
8+
9+
/**
10+
*
11+
* @return
12+
*/
13+
Ws connect() throws Exception;
14+
15+
16+
/**
17+
*
18+
* @param channelPath
19+
* @param wsListner
20+
*/
21+
<T> Ws on(String channelPath,Class<T> exceptedDataType, WsListner<T> wsListner);
22+
23+
24+
/**
25+
*
26+
* @param channelPath
27+
* @param wsListner
28+
* @return
29+
*/
30+
Ws on(String channelPath, WsListner wsListner);
31+
32+
/**
33+
*
34+
* @param channelPath
35+
* @param text
36+
*/
37+
void send(String channelPath,String text);
38+
39+
40+
/**
41+
*
42+
* @param channelPath
43+
* @param binary
44+
*/
45+
void send(String channelPath,byte[] binary);
46+
47+
/**
48+
*
49+
*/
50+
interface WsListner<T> {
51+
52+
void onEvent(String eventUri,T data);
53+
}
54+
55+
56+
class Builder {
57+
public Builder(){
58+
}
59+
60+
public WsImpl from(String websocketServerUri){
61+
return new WsImpl(websocketServerUri);
62+
}
63+
}
64+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.craftsman.websockets;
2+
3+
import android.util.Log;
4+
5+
import com.google.gson.Gson;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import de.tavendo.autobahn.Autobahn;
11+
import de.tavendo.autobahn.AutobahnConnection;
12+
13+
/**
14+
* Created by ALI SHADAÏ (Software Craftman) on 15/09/2017.
15+
*/
16+
17+
public class WsImpl implements Ws {
18+
19+
final String TAG = "Web Socket Impl";
20+
21+
final AutobahnConnection autobahnConnection = new AutobahnConnection();
22+
final List<Payload> subscriptions = new ArrayList<>();
23+
String serverUrl;
24+
25+
public WsImpl(String websocketServerUri) {
26+
serverUrl = websocketServerUri;
27+
}
28+
29+
30+
@Override
31+
public Ws connect() throws Exception {
32+
if(serverUrl == null || !serverUrl.startsWith("ws")){
33+
throw new Exception("Right server url is not provided");
34+
}
35+
autobahnConnection.connect(serverUrl, new Autobahn.SessionHandler() {
36+
@Override
37+
public void onOpen() {
38+
Log.i(TAG,"Connected");
39+
40+
for (final Payload payload: subscriptions) {
41+
//setup listener for all subscribed channel
42+
if(payload == null || payload.channel == null || payload.channel.isEmpty()) continue;
43+
else if(payload.channel.startsWith("/")) payload.channel = payload.channel.substring(1);
44+
autobahnConnection.subscribe(payload.channel, Object.class, new Autobahn.EventHandler() {
45+
@Override
46+
public void onEvent(String s, Object o) {
47+
try {
48+
payload.listner.onEvent(s,
49+
(payload.objectType != null) ?
50+
new Gson().fromJson(o.toString(),payload.objectType)
51+
: o);
52+
53+
}
54+
catch (Exception e){
55+
e.printStackTrace();
56+
}
57+
}
58+
});
59+
}
60+
}
61+
62+
@Override
63+
public void onClose(int i, String s) {
64+
Log.i(TAG,"Disconnected");
65+
}
66+
});
67+
return this;
68+
}
69+
70+
@Override
71+
public <T> Ws on(final String channelPath, final Class<T> exceptedDataType, final WsListner<T> wsListner) {
72+
73+
if(!autobahnConnection.isConnected()){
74+
subscriptions.add(new Payload<>(channelPath,exceptedDataType,wsListner));
75+
return this;
76+
}
77+
else {
78+
autobahnConnection.subscribe(channelPath, Object.class, new Autobahn.EventHandler() {
79+
@Override
80+
public void onEvent(String s, Object o) {
81+
wsListner.onEvent(s,new Gson().fromJson(o.toString(),exceptedDataType));
82+
}
83+
});
84+
}
85+
86+
return this;
87+
}
88+
89+
@Override
90+
public Ws on(String channelPath, final WsListner wsListner) {
91+
if(!autobahnConnection.isConnected()){
92+
subscriptions.add(new Payload<>(channelPath,null,wsListner));
93+
return this;
94+
}
95+
else autobahnConnection.subscribe(channelPath, Object.class, new Autobahn.EventHandler() {
96+
@Override
97+
public void onEvent(String s, Object o) {
98+
wsListner.onEvent(s, o);
99+
}
100+
});
101+
102+
return this;
103+
}
104+
105+
@Override
106+
public void send(String channelPath, String text) {
107+
108+
}
109+
110+
@Override
111+
public void send(String channelPath, byte[] binary) {
112+
113+
}
114+
115+
final private class Payload<T>{
116+
String channel;
117+
Class<T> objectType;
118+
WsListner listner;
119+
120+
121+
122+
public Payload(String channel, Class<T> objectType, WsListner listner) {
123+
this.channel = channel;
124+
this.objectType = objectType;
125+
this.listner = listner;
126+
}
127+
}
128+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">websockets</string>
3+
</resources>

0 commit comments

Comments
 (0)