Skip to content

Commit

Permalink
Merge pull request #2 from codeFighting/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
issotina authored Jul 11, 2018
2 parents 15c7dee + 1d5cb8b commit 077dc54
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 46 deletions.
17 changes: 9 additions & 8 deletions websockets/build.gradle
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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'
}
26 changes: 17 additions & 9 deletions websockets/src/main/java/com/craftsman/websockets/Ws.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
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;


/**
*
* @param channelPath
* @param wsListner
*/
<T> Ws on(String channelPath,Class<T> exceptedDataType, WsListner<T> wsListner);
<T> Ws on(String channelPath, Class<T> exceptedDataType, WsListner<T> wsListner);


/**
Expand All @@ -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<String> channelPath);

/**
*
Expand All @@ -48,7 +56,7 @@ public interface Ws {
* @param channelPath
* @param o
*/
void send(String channelPath,Object o);
void send(String channelPath, Object o);


/**
Expand All @@ -61,7 +69,7 @@ public interface Ws {
*/
interface WsListner<T> {

void onEvent(String eventUri,T data);
void onEvent(String eventUri, T data);
}


Expand All @@ -73,4 +81,4 @@ public WsImpl from(String websocketServerUri){
return new WsImpl(websocketServerUri);
}
}
}
}
111 changes: 82 additions & 29 deletions websockets/src/main/java/com/craftsman/websockets/WsImpl.java
Original file line number Diff line number Diff line change
@@ -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<Payload> 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<Payload> 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 {
Expand All @@ -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();
Expand All @@ -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);
}
}
});
Expand All @@ -74,9 +105,9 @@ public void onClose(int i, String s) {

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

if(!autobahnConnection.isConnected()){
subscriptions.add(new Payload<>(channelPath,exceptedDataType,wsListner));
if (!autobahnConnection.isConnected()) {
return this;
}
else {
Expand All @@ -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() {
Expand All @@ -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<String> channelPath) {
for (String payload : channelPath) {
unsubscribe(payload);
}
return this;
}

@Override
public void send( String text) {
if(autobahnConnection.isConnected())
Expand All @@ -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<T>{
String channel;
Class<T> objectType;
WsListner listner;


private String channel;
private Class<T> objectType;
private WsListner listner;

public Payload(String channel, Class<T> objectType, WsListner listner) {
Payload(String channel, Class<T> objectType, WsListner listner) {
this.channel = channel;
this.objectType = objectType;
this.listner = listner;
}
}
}
}
Loading

0 comments on commit 077dc54

Please sign in to comment.