Skip to content

Commit 077dc54

Browse files
authored
Merge pull request #2 from codeFighting/master
Update
2 parents 15c7dee + 1d5cb8b commit 077dc54

4 files changed

Lines changed: 242 additions & 46 deletions

File tree

websockets/build.gradle

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 25
5-
buildToolsVersion '26.0.2'
4+
compileSdkVersion 27
5+
buildToolsVersion '27.0.3'
66

77
defaultConfig {
88
minSdkVersion 14
9-
targetSdkVersion 25
9+
targetSdkVersion 27
1010
versionCode 1
1111
versionName "1.0"
1212

@@ -22,11 +22,12 @@ android {
2222
}
2323

2424
dependencies {
25-
compile fileTree(include: ['*.jar'], dir: 'libs')
26-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
implementation fileTree(include: ['*.jar'], dir: 'libs')
26+
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
2727
exclude group: 'com.android.support', module: 'support-annotations'
2828
})
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'
29+
testImplementation 'junit:junit:4.12'
30+
implementation 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
31+
implementation 'com.google.code.gson:gson:2.8.1'
32+
implementation 'org.apache.commons:commons-lang3:3.6'
3233
}

websockets/src/main/java/com/craftsman/websockets/Ws.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package com.craftsman.websockets;
22

3-
/**
4-
* Created by ALI SHADAÏ (Software Craftman) on 15/09/2017.
5-
*/
63

4+
import java.util.List;
5+
6+
@SuppressWarnings("JavaDoc")
77
public interface Ws {
88

99
/**
1010
*
1111
* @return
1212
*/
13-
Ws connect() throws Exception;
13+
Ws connect() throws Exception;
1414

1515

1616
/**
1717
*
1818
* @param channelPath
1919
* @param wsListner
2020
*/
21-
<T> Ws on(String channelPath,Class<T> exceptedDataType, WsListner<T> wsListner);
21+
<T> Ws on(String channelPath, Class<T> exceptedDataType, WsListner<T> wsListner);
2222

2323

2424
/**
@@ -27,7 +27,15 @@ public interface Ws {
2727
* @param wsListner
2828
* @return
2929
*/
30-
Ws on(String channelPath, WsListner wsListner);
30+
Ws on(String channelPath, WsListner wsListner);
31+
32+
/**
33+
* @param channelPath
34+
* @return
35+
*/
36+
Ws unsubscribe(String channelPath);
37+
38+
Ws unsubscribe(List<String> channelPath);
3139

3240
/**
3341
*
@@ -48,7 +56,7 @@ public interface Ws {
4856
* @param channelPath
4957
* @param o
5058
*/
51-
void send(String channelPath,Object o);
59+
void send(String channelPath, Object o);
5260

5361

5462
/**
@@ -61,7 +69,7 @@ public interface Ws {
6169
*/
6270
interface WsListner<T> {
6371

64-
void onEvent(String eventUri,T data);
72+
void onEvent(String eventUri, T data);
6573
}
6674

6775

@@ -73,4 +81,4 @@ public WsImpl from(String websocketServerUri){
7381
return new WsImpl(websocketServerUri);
7482
}
7583
}
76-
}
84+
}

websockets/src/main/java/com/craftsman/websockets/WsImpl.java

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,63 @@
11
package com.craftsman.websockets;
22

3+
import android.os.Handler;
34
import android.util.Log;
45

56
import com.google.gson.Gson;
67

8+
import org.apache.commons.lang3.StringUtils;
9+
710
import java.util.ArrayList;
811
import java.util.List;
912

1013
import de.tavendo.autobahn.Autobahn;
1114
import de.tavendo.autobahn.AutobahnConnection;
1215

13-
/**
14-
* Created by ALI SHADAÏ (Software Craftman) on 15/09/2017.
15-
*/
16-
16+
@SuppressWarnings("unchecked")
1717
public class WsImpl implements Ws {
18+
private final String TAG = "Web Socket Impl";
19+
private final List<Payload> subscriptions = new ArrayList<>();
20+
private Handler mainHandler = new Handler();
21+
private AutobahnConnection autobahnConnection = new AutobahnConnection();
22+
private String serverUrl;
23+
private Runnable handleSocketReconnection = new Runnable() {
24+
@Override
25+
public void run() {
26+
try {
27+
if (autobahnConnection != null && !autobahnConnection.isConnected())
28+
connect();
29+
} catch (Exception e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
};
1834

19-
final String TAG = "Web Socket Impl";
20-
21-
AutobahnConnection autobahnConnection = new AutobahnConnection();
22-
final List<Payload> subscriptions = new ArrayList<>();
23-
String serverUrl;
24-
25-
public WsImpl(String websocketServerUri) {
35+
WsImpl(String websocketServerUri) {
2636
serverUrl = websocketServerUri;
2737
}
2838

39+
public void changeSocketURI(String host, String port) throws Exception {
40+
41+
if (serverUrl != null && !serverUrl.isEmpty()) {
42+
43+
if (autobahnConnection != null && autobahnConnection.isConnected()) {
44+
end();
45+
}
46+
47+
String[] spliter = serverUrl.split(":");
48+
if (host != null && !host.isEmpty()) {
49+
spliter[1] = "//" + host;
50+
}
51+
52+
if (port != null && !port.isEmpty()) {
53+
spliter[2] = port;
54+
}
55+
56+
serverUrl = StringUtils.join(spliter, ":");
57+
58+
connect();
59+
}
60+
}
2961

3062
@Override
3163
public Ws connect() throws Exception {
@@ -49,7 +81,6 @@ public void onEvent(String s, Object o) {
4981
(payload.objectType != null) ?
5082
new Gson().fromJson(o.toString(),payload.objectType)
5183
: o);
52-
5384
}
5485
catch (Exception e){
5586
e.printStackTrace();
@@ -61,11 +92,11 @@ public void onEvent(String s, Object o) {
6192
@Override
6293
public void onClose(int i, String s) {
6394
//force recnnection to web socket
64-
Log.i(TAG,"Disconnected");
65-
try {
66-
if(!autobahnConnection.isConnected()) connect();
67-
} catch (Exception e) {
68-
e.printStackTrace();
95+
Log.e(TAG, "Disconnected; Code " + i);
96+
97+
if (i == 1 || i == 3 || i == 2 || i == 4 || i == 5) {
98+
mainHandler.removeCallbacks(handleSocketReconnection);
99+
mainHandler.postDelayed(handleSocketReconnection, 15000);
69100
}
70101
}
71102
});
@@ -74,9 +105,9 @@ public void onClose(int i, String s) {
74105

75106
@Override
76107
public <T> Ws on(final String channelPath, final Class<T> exceptedDataType, final WsListner<T> wsListner) {
108+
subscriptions.add(new Payload<>(channelPath, exceptedDataType, wsListner));
77109

78-
if(!autobahnConnection.isConnected()){
79-
subscriptions.add(new Payload<>(channelPath,exceptedDataType,wsListner));
110+
if (!autobahnConnection.isConnected()) {
80111
return this;
81112
}
82113
else {
@@ -91,10 +122,11 @@ public void onEvent(String s, Object o) {
91122
return this;
92123
}
93124

125+
94126
@Override
95127
public Ws on(String channelPath, final WsListner wsListner) {
96-
if(!autobahnConnection.isConnected()){
97-
subscriptions.add(new Payload<>(channelPath,null,wsListner));
128+
subscriptions.add(new Payload<>(channelPath, null, wsListner));
129+
if (!autobahnConnection.isConnected()) {
98130
return this;
99131
}
100132
else autobahnConnection.subscribe(channelPath, Object.class, new Autobahn.EventHandler() {
@@ -107,6 +139,29 @@ public void onEvent(String s, Object o) {
107139
return this;
108140
}
109141

142+
@Override
143+
public Ws unsubscribe(String channelPath) {
144+
for (Payload payload : subscriptions) {
145+
if (StringUtils.equals(payload.channel, channelPath)) {
146+
if (autobahnConnection != null) {
147+
if (autobahnConnection.isConnected())
148+
autobahnConnection.unsubscribe(channelPath);
149+
150+
subscriptions.remove(payload);
151+
}
152+
}
153+
}
154+
return this;
155+
}
156+
157+
@Override
158+
public Ws unsubscribe(List<String> channelPath) {
159+
for (String payload : channelPath) {
160+
unsubscribe(payload);
161+
}
162+
return this;
163+
}
164+
110165
@Override
111166
public void send( String text) {
112167
if(autobahnConnection.isConnected())
@@ -129,21 +184,19 @@ public void send(String channelPath, Object o) {
129184
public void end() {
130185
if(autobahnConnection != null && autobahnConnection.isConnected()) {
131186
autobahnConnection.unsubscribe();
132-
autobahnConnection = null;
187+
autobahnConnection.disconnect();
133188
}
134189
}
135190

136191
final private class Payload<T>{
137-
String channel;
138-
Class<T> objectType;
139-
WsListner listner;
140-
141-
192+
private String channel;
193+
private Class<T> objectType;
194+
private WsListner listner;
142195

143-
public Payload(String channel, Class<T> objectType, WsListner listner) {
196+
Payload(String channel, Class<T> objectType, WsListner listner) {
144197
this.channel = channel;
145198
this.objectType = objectType;
146199
this.listner = listner;
147200
}
148201
}
149-
}
202+
}

0 commit comments

Comments
 (0)