11package com .craftsman .websockets ;
22
3+ import android .os .Handler ;
34import android .util .Log ;
45
56import com .google .gson .Gson ;
67
8+ import org .apache .commons .lang3 .StringUtils ;
9+
710import java .util .ArrayList ;
811import java .util .List ;
912
1013import de .tavendo .autobahn .Autobahn ;
1114import de .tavendo .autobahn .AutobahnConnection ;
1215
13- /**
14- * Created by ALI SHADAÏ (Software Craftman) on 15/09/2017.
15- */
16-
16+ @ SuppressWarnings ("unchecked" )
1717public 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