ERROR Error: Trying to link invalid 'this' to a Java object


#1

Hello,

I am trying to implement https://github.com/socketio/socket.io-client-java in NativeScript as bellow:

let manager = new io.socket.client.Manager(android.net.Uri("http://192.168.43.94:3001"));
let socket = manager.socket("/status");
socket.connect();

But I am getting this error:

JS: ERROR Error: Trying to link invalid 'this' to a Java object

Any idea to resolve the problem?

Thanks


#2

The Uri isn’t correctly constructed. Try this: android.net.Uri.parse("http://192.168.43.94:3001") (parse is a static method of the Uri class).


#3

Thanks for your reply. I have changed it like this:

let manager = new io.socket.client.Manager(android.net.Uri.parse("http://192.168.43.94:3001"));
let socket = manager.socket("/status");
socket.connect();

Now I am getting this error:

JS: ERROR Error: java.lang.Exception: Failed resolving constructor for class 'io.socket.client.Manager' with 1 parameters. Check the number and type of arguments.
JS: Primitive types need to be manually wrapped in their respective Object wrappers.
JS: If you are creating an instance of an inner class, make sure to always provide reference to the outer `this` as the first argument.
JS:     com.tns.Runtime.resolveConstructorSignature(Runtime.java:1043)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1101)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:983)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:970)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:954)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:946)
JS:     com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:18)
JS:     android.app.Activity.performCreate(Activity.java:6679)
JS:     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
JS:     android.app.ActivityThread.performLaunchAc...

#4

Most time in Android Uri refers to android.net.Uri but the socket io library uses the URI from java namespace. So you must create a instance like java.net.URI("http://192.168.43.94:3001").


#5

@manojdcoder is right, I just noticed as well. https://github.com/socketio/socket.io-client-java/blob/964a66fc1c57042a37301ec426ffef266bfc0f56/src/main/java/io/socket/client/Manager.java#L121


#6

Any hints to resolve the problem?

let manager = new io.socket.client.Manager(java.net.URI("http://192.168.43.94:3001"));
let socket = manager.socket("/status");
socket.connect();

JS: ERROR Error: Trying to link invalid 'this' to a Java object


#7

Sorry, I missed new keyword, an instance should always be created with new keyboard.

let manager = new io.socket.client.Manager(new java.net.URI("http://192.168.43.94:3001"));

#8

Thank you so much !! It’s working perfectly now :slight_smile:


#9

@manojdcoder I will need little bit help for marshaling Listener .

socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

  @Override
  public void call(Object... args) {
    socket.emit("foo", "hi");
    socket.disconnect();
  }

})

So, I tried like this:

socket.on(socket.EVENT_CONNECT, new io.socket.emitter.Emitter.Listener({
            call: function(args: any){
                console.log('connect');
            }
        }))

But got error:
JS: ERROR Error: Cannot convert object to Lio/socket/emitter/Emitter$Listener; at index 1

Even tried this:

export class EmitterListener extends (io.socket.emitter.Emitter.Listener as { new(): any; }) {

    constructor() {
        super();
        return global.__native(this);
    }

    public call(args: any): void {
        console.log("test")
    }
}

then
socket.on(socket.EVENT_CONNECT, new EmitterListener())

But got this error:

JS: ERROR Error: java.lang.ClassNotFoundException: com.tns.gen.io.socket.emitter.Emitter_Listener_component_179_28_EmitterListener
JS:     java.lang.Class.classForName(Native Method)
JS:     java.lang.Class.forName(Class.java:400)
JS:     java.lang.Class.forName(Class.java:326)
JS:     com.tns.Runtime.getClassForName(Runtime.java:1023)
JS:     com.tns.ClassResolver.resolveClass(ClassResolver.java:27)
JS:     com.tns.Runtime.resolveClass(Runtime.java:593)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1101)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:983)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:970)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:954)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:946)
JS:     com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:18)
JS:     android.app.Activity.performCreate(Activity.java:6679)
JS:     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
JS:     android.app.ActivityThread.performLaunchActivity(A...

#10

OK, seems like this is working:

socket.on("connect", new io.socket.emitter.Emitter.Listener({
            call(res) {
                console.log("connect");
            }
        }))