Please help with android connecting to wamp server

HI there. I have crossbar 20.12.3 running on a VM addressed 6.6.6.4. I have a web transport on port 9001 with static auth at /ws_static_auth. I have several Python clients running on the host and connecting to this server. I’m running this on a Pixel 4 XL API 29 emulator.

I just cannot get this to work in an Android app. I started the basic two fragment starter app with Android so that I could test this. I have tried the following in both MainActivity onCreate and FragmentOne onCreate:

ExecutorService executor = Executors.newSingleThreadExecutor();

Session session = new Session(executor);
session.addOnConnectListener(this::onConnectListener);
session.addOnJoinListener(this::demonstrateCall);
session.addOnLeaveListener(this::onLeaveListener);
session.addOnDisconnectListener(this::onDisconnectListener);

Client client = new Client(session, "ws://6.6.6.4:9001/ws_static_auth", "myrealm",executor);

CompletableFuture<ExitInfo> exitFuture = client.connect();
try {
    ExitInfo exitInfo = exitFuture.get();
} catch (ExecutionException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

This blocks the fragment and I see no signs of connection on the server. Granted there is no craauth here but I should see a connect at least. I also tried putting the client connect in a full Runnable to submit to the executor service:

ExecutorService executor = Executors.newSingleThreadExecutor();
Runnable runnableTask = () -> {
    Log.i("GHGH","HERE");
    while(true) {
        Log.i("GHGH", "HERE2");
        Session session = new Session();
        // Add all onJoin listeners
        session.addOnJoinListener(this::demonstrateCall);
        session.addOnConnectListener(this::onConnectListener);
        session.addOnDisconnectListener(this::onDisconnectListener);
        session.addOnLeaveListener(this::onLeaveListener);
        session.addOnReadyListener(this::onReadyListener);
        session.addOnUserErrorListener(this::onUserErrorListener);
        IAuthenticator authenticator = new ChallengeResponseAuth("android-test-client", "android-test-client");
        wampClient = new Client(session, "ws://6.6.6.4:9001/ws_static_auth", "myrealm", new ChallengeResponseAuth("android-test-client", "android-test-client"));
        CompletableFuture<ExitInfo> exitInfoCompletableFuture = wampClient.connect();
        ExitInfo result = null;
        exitInfoCompletableFuture.join();
        try {
            result = exitInfoCompletableFuture.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("RESULT:"+result);
        try {
            TimeUnit.MILLISECONDS.sleep(60000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
List<Runnable> runnableTasks = new ArrayList<>();
runnableTasks.add(runnableTask);
executor.submit(runnableTask);

This is definitely running in the background, but the Client just calls the disconnect immediately and the ExitInfo doesn’t provide any useful info. Furthermore there is no sign of connection in crossbar.

Can anyone please tell me what I am doing wrong here? Thank you.

did you try the example we provide?

1 Like

I am curious to know why are you using the Thread executor ? Our WAMP implementation takes care of that internally, so if you run our example code, it won’t run in the main thread or block the UI.

You can even call UI-changing code in our callbacks because internally we “notify” the main thread for events using os.android.Handler.post()

1 Like

I first tried it without the executor but it blocks the fragment from drawing with this code:

Session session = new Session();
session.addOnConnectListener(this::onConnectListener);
session.addOnJoinListener(this::demonstrateCall);
session.addOnLeaveListener(this::onLeaveListener);
session.addOnDisconnectListener(this::onDisconnectListener);

IAuthenticator authenticator = new ChallengeResponseAuth("android-test-client", "android-test-client");
Client client = new Client(session, "ws://6.6.6.4:9001/ws_static_admin", "myrealm", authenticator);

CompletableFuture<ExitInfo> exitFuture = client.connect();
try {
    ExitInfo exitInfo = exitFuture.get();
} catch (ExecutionException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

I’m not sure if I need the exitFuture.get() or not but without the get() it runs but doesn’t connect at all.

Then I scoured the internet for any example I could find and many people were using the executor so I was trying that.

I did set up the demo-gallery crossbar server on 6.6.6.4 and I couldn’t connect with the demo app at all either.

The problem is with that line. That’s a blocking call, you should rather call

exitFuture.whenComplete(exitCode, throwable) -> {
}

The code in that runnable will only be called when your connection fails and that is not a blocking call.

Thank you for your quick replies. Now I have this but it is still not showing any sign of connecting:

IAuthenticator authenticator = new ChallengeResponseAuth("android-test-client", "android-test-client");
Client client = new Client(session, "ws://6.6.6.4:9001/ws_static_admin", "myrealm", authenticator);

CompletableFuture<ExitInfo> exitFuture = client.connect();
exitFuture.whenComplete((exitCode, throwable) -> {

});

Can you share the logcat for the time when you try to connect to crossbar, that may have some pointers.

Nothing there:

2021-03-08 13:01:56.647 12912-12912/biz.qjumper.client.wamp_test I/GHGH: BEFORE WAMP CONNECTION
2021-03-08 13:01:56.674 12912-12912/biz.qjumper.client.wamp_test I/GHGH: AFTER WAMP CONNECTION
2021-03-08 13:01:56.720 12912-12912/biz.qjumper.client.wamp_test I/GHGH: DISCONNECT

The DISCONNECT is coming from my onDisconnect.

Interesting…

Can you tell if you are running your app on an Android phone or Android Emulator (emulator can have a hard time talking to your VM, because it doesn’t know what 6.6.6.4 is).

One other “thing” would be to use autobahn-java with debug flag enabled, unfortunately we don’t have that as a runtime flag currently, something I need to fix. One way of doing that today would be to run autobahn-java from source and set this line to true https://github.com/crossbario/autobahn-java/blob/master/autobahn/src/main/java/io/crossbar/autobahn/utils/Globals.java#L16 (it’s set to false in released version).

I was running this on the emulator but my phone is doing the same thing:

2021-03-08 13:17:34.975 29761-29761/biz.qjumper.client.wamp_test I/GHGH: BEFORE WAMP CONNECTION
2021-03-08 13:17:34.996 29761-29761/biz.qjumper.client.wamp_test I/GHGH: AFTER WAMP CONNECTION
2021-03-08 13:17:35.041 29761-29761/biz.qjumper.client.wamp_test I/ViewRootImpl@f95eea9[MainActivity]: setView = com.android.internal.policy.DecorView@b4529d8 TM=true MM=false
2021-03-08 13:17:35.045 29761-29761/biz.qjumper.client.wamp_test I/GHGH: DISCONNECT

I am not a java expert, but I will try to figure out how to run from source with debug.

I guess your mobile phone cannot talk to the VM either unless you do port forwarding.

I can access 6.6.6.4:9001/ws_static_auth in the browser on both my phone and the mac I’m running the emulator on though.

FYI, I used a hosts file entry in the emulator instead of the IP and it gave me an exception about INTERNET permission, so I thought “ah ha”, but I added the permission and it is still doing the same thing.

Added this to manifest:

<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Can you try to uninstall the android app and reinstall app. There is a bug in Android that causes apps to not have internet access if they are first run without the above line. Only a reinstall fixes that.

Is is working both from my phone and emulator now with IP. That was it, thank you. Strange, I wiped my emulator and it didn’t work once but then I tried it on my phone and it started working.

I would have expected Android to give an error, but at least it is working now.

2 Likes