Java Question returning value from raiseEvent, raiseEvent2, or raiseEventFromDifferentThread

rdkartono

Member
Licensed User
Longtime User
In java, i write a library, something like this:
Java code:
_client.on(eventname, new Emitter.Listener() {
                    @Override
                    public void call(Object... args) {
                        // some codes
                        if (debugmode) raise_log("Event "+eventname+" value is Object, raising with Object");               
                        // first try using raiseEvent
                        //returndata = bax.raiseEvent(Me, eventtoraise, new Object[] {valuedata});
                        // second try using raiseEvent2
                        //returndata = bax.raiseEvent2(Me, false, eventtoraise, false, new Object[] {valuedata});
                        // third try using raiseEventFromDifferentThread
                        returndata = bax.raiseEventFromDifferentThread(Me, null, 0, eventtoraise, false, new Object[] {valuedata});
                        if (debugmode) raise_log("Return Data = "+returndata);
                    }
}

Then in B4A, I write event something like this :
B4A Code:
Private Sub socket_app_version(value As String) As String
    Log("value = "&value)
    Dim reply As String = "Version Different"
    Return reply
End Sub

The B4A event is called on Debug and Release Mode.

returndata in java, only sucess (have value) in Release mode, using raiseEvent and raiseEvent2, but not on raiseEventFromDifferentThread (null)
On debug mode, returndata in Java always null.

Any suggestion ?
 

rdkartono

Member
Licensed User
Longtime User
So far, there is no "unexpected event" in log , or unfiltered log.
I can send the unfiltered log personally to you if you want , Erel .

And I haven't use #RaisesSynchronousEvents annotation, because app is not crashing.
 

rdkartono

Member
Licensed User
Longtime User
I put #RaisesSynchronousEvents: socket_app_version on top of the class file, yet still same result.
Release Mode, returndata have value
Debug Mode, returndata is null.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

rdkartono

Member
Licensed User
Longtime User
Skeleton for the java code is below
Java Code:
public boolean Add_Event(final String eventname, boolean debugmode) {
    
    // some codes
    _client.on(eventname, new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            // some codes
            if (debugmode) raise_log("Event "+eventname+" value is Object, raising with Object");
            returndata = bax.raiseEvent(Me, eventtoraise, new Object[] {valuedata});
            raise_log("Return Data = "+returndata);
        }
    });
}

Experiment 1 : dont put RaisesSynchronousEvents --> Debug = null, Release = have value
Experiment 2 : put @BA.RaisesSynchronousEvents above public void call --> result same as experiment 1
Experiment 2:
public boolean Add_Event(final String eventname, boolean debugmode) {
    // some codes
    _client.on(eventname, new Emitter.Listener() {
        @BA.RaisesSynchronousEvents
        @Override
        public void call(Object... args) {
            // some codes
            if (debugmode) raise_log("Event "+eventname+" value is Object, raising with Object");
            returndata = bax.raiseEvent(Me, eventtoraise, new Object[] {valuedata});
            raise_log("Return Data = "+returndata);
        }
    });
Experiment 3 : put @BA.RaisesSynchronousEvents above public boolean Add_Event --> result same as experiment 1
experiment 3:
@BA.RaisesSynchronousEvents
public boolean Add_Event(final String eventname, boolean debugmode) {
    // some codes
    _client.on(eventname, new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            // some codes
            if (debugmode) raise_log("Event "+eventname+" value is Object, raising with Object");
            returndata = bax.raiseEvent(Me, eventtoraise, new Object[] {valuedata});
            raise_log("Return Data = "+returndata);
        }
    });
Experiment 4 : put on both of them --> result same as experiment 1
experiment 4:
@BA.RaisesSynchronousEvents
public boolean Add_Event(final String eventname, boolean debugmode) {
    // some codes
    _client.on(eventname, new Emitter.Listener() {
        @BA.RaisesSynchronousEvents
        @Override
        public void call(Object... args) {
            // some codes
            if (debugmode) raise_log("Event "+eventname+" value is Object, raising with Object");
            returndata = bax.raiseEvent(Me, eventtoraise, new Object[] {valuedata});
            raise_log("Return Data = "+returndata);
        }
    });

So far, the @BA.RaisesSynchronousEvents annotation has no effect.

If helps,
I use JDK 1.8.0_361 for Java Library.
I compile using Eclipse Version: 2023-12 (4.30.0). Prefer this than SLC utility.
I use JDK 14.0.1 for B4A
I use B4A version 12.80
 

rdkartono

Member
Licensed User
Longtime User
Sorry for late reply . Yesterday was national holiday.

1. Event will be raised on socket.io-client event . I use jar from here.
2. Well, I am not sure. For one, I know the java event is a callback type. then I use B4A RaiseEvent function.
Still, the thing is Release mode can give return object correctly, while Debug mode is always null.
 

rdkartono

Member
Licensed User
Longtime User
Socket.io is different than Socket.
Socket.io is websocket-like communication.

for now, I have no other choice than compiling in Release Mode , and use #BridgeLogger: true to get logs .

If I have time, I will try to look for RaiseEvent source code in github to check
 

rdkartono

Member
Licensed User
Longtime User
Thank you for your suggestion Erel, however I need to connect with a server implementing socket.io communication, so its best if I used already made socket.io jar.

Today I create my own SocketIoObject, use it as bax.raiseEventFromDifferentThread object, and implement wait() and notify() in java.
so far, it can run on debug mode and still get reply from B4A.
 
Top