Java Question Simple Compiler Library and ba.raiseEvent

b4xscripter

Member
Licensed User
Longtime User
Hello, can anyone explain me with this example why my event is not called from B4A?

using the Simple Library Compiler I could compile it:

B4X:
package anywheresoftware.b4a.SocketIOClient;

import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.*;
import anywheresoftware.b4a.BA;
import android.telephony.*;



import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.ParseException;
import io.socket.*;



@Permissions(values={"android.permission.INTERNET"})
@ShortName("SocketIOClient")
@DependsOn(values={"json-org","json-simple-1.1.1","WebSocket","socket2"})
@ActivityObject
@Events(values={"OnConnect(json as String)"})



public class SocketIOClient {
private SocketIO socket;
private BA ba;
private String eventName;
public void connect(String host) throws MalformedURLException {
  
      this.socket = new SocketIO(host);
          socket.connect(new IOCallback() {
            @Override
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {
                    System.out.println("Server said:" + json.toString(2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onMessage(String data, IOAcknowledge ack) {
                System.out.println("Server said: " + data);
            }

            @Override
            public void onError(SocketIOException socketIOException) {
                System.out.println("an Error occured");
                socketIOException.printStackTrace();
            }

            @Override
            public void onDisconnect() {
                System.out.println("Connection terminated.");
            }

            @Override
            public void onConnect() {
                System.out.println("Connection established");
                ba.raiseEvent(this, eventName + "_onconnect","OK");
            }

            @Override
            public void on(String event, IOAcknowledge ack, Object... args) {
                System.out.println("Server triggered event '" + event + "'");
            }
                        });
                                }
public void initialize(BA ba, String EventName) throws MalformedURLException       
{
          this.ba = ba;
          this.eventName = EventName.toLowerCase(BA.cul);
}

}


Here goes my question because I don't understand very well what name should I use in my case to get the event working.....


B4X:
@Events(values={"OnConnect(json as String)"})

Should I put this one?

B4X:
Sub OnConnect(valor As String)
Msgbox("connected!","info") 

End Sub



Or this one, supposing that I initialize it this way:


B4X:
     Sub Globals

    Dim socket As SocketIOClient

End Sub

Sub Activity_Create(FirstTime As Boolean)
     DisableStrictMode
     socket.initialize("socket")
     socket.connect("http://192.168.1.3:8080/")

End Sub

Sub socket_onconnect(valor As String)
Msgbox("connected!","info") 

End Sub


I supposed this last name because of the:

B4X:
ba.raiseEvent(this, eventName + "_onconnect","OK");


Could anyone help me?

Thank you!!!!
 

b4xscripter

Member
Licensed User
Longtime User
Yes, Erel, I think so...
But o linked it to the SLC because of the code:

@Events(values={"OnConnect(json as String)"})


What shoul I put there to get the event Socket_OnConnect? Should it be the same as in
ba.raiseEvent(this, eventName + "_onconnect","OK");?


Thank you! Great support!
 

b4xscripter

Member
Licensed User
Longtime User
Yes, Erel, I did everything according to the documentation, but my event is not firing....

B4X:
package anywheresoftware.b4a.SocketIOClient;

import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.*;
import anywheresoftware.b4a.BA;
import android.telephony.*;



import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.ParseException;
import io.socket.*;



@Permissions(values={"android.permission.INTERNET"})
@ShortName("SocketIOClient")
@DependsOn(values={"json-org","json-simple-1.1.1","WebSocket","socket2"})
@ActivityObject
@Events(values={"OnConnect (json as String)"})



public class SocketIOClient {
private SocketIO socket;
private BA ba;
private String eventName;
public void connect(String host) throws MalformedURLException {
  
      this.socket = new SocketIO(host);
          socket.connect(new IOCallback() {
            @Override
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {
                    System.out.println("Server said:" + json.toString(2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onMessage(String data, IOAcknowledge ack) {
                System.out.println("Server said: " + data);
            }

            @Override
            public void onError(SocketIOException socketIOException) {
                System.out.println("an Error occured");
                socketIOException.printStackTrace();
            }

            @Override
            public void onDisconnect() {
                System.out.println("Connection terminated.");
            }

            @Override
            public void onConnect() {
                System.out.println("Connection established");
                ba.raiseEvent(null, eventName + "_onconnect","OK");
            }

            @Override
            public void on(String event, IOAcknowledge ack, Object... args) {
                System.out.println("Server triggered event '" + event + "'");
            }
                        });
                                }
public void initialize(BA ba, String EventName) throws MalformedURLException       
{
          this.ba = ba;
          this.eventName = EventName.toLowerCase(BA.cul);
}
public void emit(String event,String jsonStr) throws ParseException {

                        org.json.simple.JSONObject jsonConf = null;
                        jsonConf = (org.json.simple.JSONObject)new JSONParser().parse(jsonStr);
                        socket.emit("joinCloud",jsonConf);             
}
}



I compile the library but in the B4A code I don't get this event fired...


B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.


End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim socket As SocketIOClient

End Sub

Sub Activity_Create(FirstTime As Boolean)
     DisableStrictMode
     socket.initialize("OnConnect")
     socket.connect("http://192.168.8.100:8080/")
     socket.emit("joinCloud","{}")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub OnConnect(valor As String)
Msgbox("connected!","info")   ' This event is not fired.....

End Sub


Sub DisableStrictMode
   Dim jo As JavaObject
   jo.InitializeStatic("android.os.Build.VERSION")
   If jo.GetField("SDK_INT") > 9 Then
     Dim policy As JavaObject
     policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
     policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
     Dim sm As JavaObject
     sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
   End If
End Sub

Where am I wrong?

P.S. I uploaded the libraries needed to compile this project.

Thank you in advance!!!!


Best regards
 

Attachments

  • WebSocket.jar
    91.4 KB · Views: 261
  • socket2.jar
    55.6 KB · Views: 255
  • json-org.jar
    35.6 KB · Views: 250
  • json-simple-1.1.1.jar
    23.2 KB · Views: 248
Last edited:

b4xscripter

Member
Licensed User
Longtime User
Erel, the problem is that I need the library for socket.io . It seems that the existing library has some problems with communication with node.js (express.js) server with socket.io. So it is why I wanted to wrap the socket.io java library for B4A.

Anyway, I am really impressed for your amazing software. I just created and sold my first Android app!!!!!!!!


Thank you!!!!!


Warmest regards
 

b4xscripter

Member
Licensed User
Longtime User
about Socket IO (that is not WebSocket):

https://en.wikipedia.org/wiki/Socket.IO

Socket.IO is not a WebSocket library with fallback options to other realtime protocols. It is a custom realtime transport protocol implementation on top of other realtime protocols. Its protocol negotiation parts cause a client supporting standard WebSocket to not being able to contact a Socket.IO server. And a Socket.IO implementing client cannot talk to a non-Socket.IO based WebSocket or Long Polling Comet server. Therefore Socket.IO requires using the Socket.IO libraries on both client and server side.
 
Top