diff --git a/websockets/build.gradle b/websockets/build.gradle index 8541fc8..7316d34 100644 --- a/websockets/build.gradle +++ b/websockets/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 25 - buildToolsVersion '26.0.2' + compileSdkVersion 27 + buildToolsVersion '27.0.3' defaultConfig { minSdkVersion 14 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 1 versionName "1.0" @@ -22,11 +22,12 @@ android { } dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') - androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + implementation fileTree(include: ['*.jar'], dir: 'libs') + androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - testCompile 'junit:junit:4.12' - compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13' - compile 'com.google.code.gson:gson:2.8.1' + testImplementation 'junit:junit:4.12' + implementation 'org.codehaus.jackson:jackson-mapper-asl:1.9.13' + implementation 'com.google.code.gson:gson:2.8.1' + implementation 'org.apache.commons:commons-lang3:3.6' } diff --git a/websockets/src/main/java/com/craftsman/websockets/Ws.java b/websockets/src/main/java/com/craftsman/websockets/Ws.java index d42abee..248c308 100644 --- a/websockets/src/main/java/com/craftsman/websockets/Ws.java +++ b/websockets/src/main/java/com/craftsman/websockets/Ws.java @@ -1,16 +1,16 @@ package com.craftsman.websockets; -/** - * Created by ALI SHADAÏ (Software Craftman) on 15/09/2017. - */ +import java.util.List; + +@SuppressWarnings("JavaDoc") public interface Ws { /** * * @return */ - Ws connect() throws Exception; + Ws connect() throws Exception; /** @@ -18,7 +18,7 @@ public interface Ws { * @param channelPath * @param wsListner */ - Ws on(String channelPath,Class exceptedDataType, WsListner wsListner); + Ws on(String channelPath, Class exceptedDataType, WsListner wsListner); /** @@ -27,7 +27,15 @@ public interface Ws { * @param wsListner * @return */ - Ws on(String channelPath, WsListner wsListner); + Ws on(String channelPath, WsListner wsListner); + + /** + * @param channelPath + * @return + */ + Ws unsubscribe(String channelPath); + + Ws unsubscribe(List channelPath); /** * @@ -48,7 +56,7 @@ public interface Ws { * @param channelPath * @param o */ - void send(String channelPath,Object o); + void send(String channelPath, Object o); /** @@ -61,7 +69,7 @@ public interface Ws { */ interface WsListner { - void onEvent(String eventUri,T data); + void onEvent(String eventUri, T data); } @@ -73,4 +81,4 @@ public WsImpl from(String websocketServerUri){ return new WsImpl(websocketServerUri); } } -} +} \ No newline at end of file diff --git a/websockets/src/main/java/com/craftsman/websockets/WsImpl.java b/websockets/src/main/java/com/craftsman/websockets/WsImpl.java index 025b729..063dcb1 100644 --- a/websockets/src/main/java/com/craftsman/websockets/WsImpl.java +++ b/websockets/src/main/java/com/craftsman/websockets/WsImpl.java @@ -1,31 +1,63 @@ package com.craftsman.websockets; +import android.os.Handler; import android.util.Log; import com.google.gson.Gson; +import org.apache.commons.lang3.StringUtils; + import java.util.ArrayList; import java.util.List; import de.tavendo.autobahn.Autobahn; import de.tavendo.autobahn.AutobahnConnection; -/** - * Created by ALI SHADAÏ (Software Craftman) on 15/09/2017. - */ - +@SuppressWarnings("unchecked") public class WsImpl implements Ws { + private final String TAG = "Web Socket Impl"; + private final List subscriptions = new ArrayList<>(); + private Handler mainHandler = new Handler(); + private AutobahnConnection autobahnConnection = new AutobahnConnection(); + private String serverUrl; + private Runnable handleSocketReconnection = new Runnable() { + @Override + public void run() { + try { + if (autobahnConnection != null && !autobahnConnection.isConnected()) + connect(); + } catch (Exception e) { + e.printStackTrace(); + } + } + }; - final String TAG = "Web Socket Impl"; - - AutobahnConnection autobahnConnection = new AutobahnConnection(); - final List subscriptions = new ArrayList<>(); - String serverUrl; - - public WsImpl(String websocketServerUri) { + WsImpl(String websocketServerUri) { serverUrl = websocketServerUri; } + public void changeSocketURI(String host, String port) throws Exception { + + if (serverUrl != null && !serverUrl.isEmpty()) { + + if (autobahnConnection != null && autobahnConnection.isConnected()) { + end(); + } + + String[] spliter = serverUrl.split(":"); + if (host != null && !host.isEmpty()) { + spliter[1] = "//" + host; + } + + if (port != null && !port.isEmpty()) { + spliter[2] = port; + } + + serverUrl = StringUtils.join(spliter, ":"); + + connect(); + } + } @Override public Ws connect() throws Exception { @@ -49,7 +81,6 @@ public void onEvent(String s, Object o) { (payload.objectType != null) ? new Gson().fromJson(o.toString(),payload.objectType) : o); - } catch (Exception e){ e.printStackTrace(); @@ -61,11 +92,11 @@ public void onEvent(String s, Object o) { @Override public void onClose(int i, String s) { //force recnnection to web socket - Log.i(TAG,"Disconnected"); - try { - if(!autobahnConnection.isConnected()) connect(); - } catch (Exception e) { - e.printStackTrace(); + Log.e(TAG, "Disconnected; Code " + i); + + if (i == 1 || i == 3 || i == 2 || i == 4 || i == 5) { + mainHandler.removeCallbacks(handleSocketReconnection); + mainHandler.postDelayed(handleSocketReconnection, 15000); } } }); @@ -74,9 +105,9 @@ public void onClose(int i, String s) { @Override public Ws on(final String channelPath, final Class exceptedDataType, final WsListner wsListner) { + subscriptions.add(new Payload<>(channelPath, exceptedDataType, wsListner)); - if(!autobahnConnection.isConnected()){ - subscriptions.add(new Payload<>(channelPath,exceptedDataType,wsListner)); + if (!autobahnConnection.isConnected()) { return this; } else { @@ -91,10 +122,11 @@ public void onEvent(String s, Object o) { return this; } + @Override public Ws on(String channelPath, final WsListner wsListner) { - if(!autobahnConnection.isConnected()){ - subscriptions.add(new Payload<>(channelPath,null,wsListner)); + subscriptions.add(new Payload<>(channelPath, null, wsListner)); + if (!autobahnConnection.isConnected()) { return this; } else autobahnConnection.subscribe(channelPath, Object.class, new Autobahn.EventHandler() { @@ -107,6 +139,29 @@ public void onEvent(String s, Object o) { return this; } + @Override + public Ws unsubscribe(String channelPath) { + for (Payload payload : subscriptions) { + if (StringUtils.equals(payload.channel, channelPath)) { + if (autobahnConnection != null) { + if (autobahnConnection.isConnected()) + autobahnConnection.unsubscribe(channelPath); + + subscriptions.remove(payload); + } + } + } + return this; + } + + @Override + public Ws unsubscribe(List channelPath) { + for (String payload : channelPath) { + unsubscribe(payload); + } + return this; + } + @Override public void send( String text) { if(autobahnConnection.isConnected()) @@ -129,21 +184,19 @@ public void send(String channelPath, Object o) { public void end() { if(autobahnConnection != null && autobahnConnection.isConnected()) { autobahnConnection.unsubscribe(); - autobahnConnection = null; + autobahnConnection.disconnect(); } } final private class Payload{ - String channel; - Class objectType; - WsListner listner; - - + private String channel; + private Class objectType; + private WsListner listner; - public Payload(String channel, Class objectType, WsListner listner) { + Payload(String channel, Class objectType, WsListner listner) { this.channel = channel; this.objectType = objectType; this.listner = listner; } } -} +} \ No newline at end of file diff --git a/websockets/websockets.iml b/websockets/websockets.iml new file mode 100644 index 0000000..2689591 --- /dev/null +++ b/websockets/websockets.iml @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file