Hi,
I am porting a Godot 3 app. The code for the signal was generated when I have upgraded my project. I have a websocket client, and try to connect reception of a message to the main.gd, in order to be able to parse it:

class_name WebSocketClient

signal message_received(string: Variant)
...
func poll() -> void
...
. message_received.emit(get_message())

This part runs well, The last line is called (I see it with a break point)

on the reception side (Main.gd), I have this

_websocketclient = WebSocketClient.new()
...
func _ready():
_webocketclient.message_received.connect(Callable(onReceiveSocket))
...
func onReceiveSocket(string):
. ...

onReceiveSocket(string) is never called, no error shows up when app runs.
I don't know wether connect need an extra parameter in definition, like

_webocketclient.message_received.connect(Callable(onReceiveSocket).bind(msg))

But 'msg' is not declared in scope, so...

  • Toxe replied to this.

    _webocketclient.message_received.connect(Callable(onReceiveSocket))
    can be changed to:
    _webocketclient.message_received.connect(onReceiveSocket)

    Since the parameter is in the signal declaration, you don't need to bind it in the connect call.

    But that doesn't help with the problem.

    moonmoon27 Looks good to me. Although .connect(onReceiveSocket) is enough, you can drop the Callable(onReceiveSocket) as a function is already a Callable.

    But that wouldn't be the issue here.

    Can you strip down your project into a small test project that shows the exact problem and upload it here? (Or your whole project if that's possible and it isn't too big.)

    Here is my project, I stripped it down the best I can.

    smsgodot.zip
    20kB

    But you will need a way to send websocket...
    I have a Qt-app that send datas to the client as soon as it connects to server.
    So, the 'message_received' signal is emitted from WebSocketClient, poll() line 76.
    I expect to 'onReceiveSocket' to be called in Main.gd, but it never happens...
    Thanks for the help

    • Toxe replied to this.

      I inverstigate a little more, and a thing that surprise me is that, when debugging, code of websocketclient.gd is run 3 times:

      1. When I call a

      _websocketClient = WebSocketClient.new()

      1. Because it is in the scene ?

      2. No clue, but this time it makes an error:

      So I do not really understand the order of instanciation, anybody could explain me what happened here?
      Thanks,

      moonmoon27 I did not notice that you posted your project.

      I found your initial problem. In your Main.gd script in _ready you create a new WebSocketClient and then connect the message_received signal:

      _websocketClient = WebSocketClient.new()
      _websocketClient.message_received.connect(onReceiveSocket)

      But you already have a WebSocketClient node in your scene. So instead of calling WebSocketClient.new() just declare your _websocketClient like this at the top, connect the signal and your callback should get called:

      @onready var _websocketClient: WebSocketClient = $WebSocketClient
      
      func _ready():
          # ...
          _websocketClient.message_received.connect(onReceiveSocket)
        12 days later

        Toxe Ok, I see that, but the 'Wsc' in my code is the old Godot 3.5 system. I need to use the other one, 'WebSocketClient.gd'.
        So I have tried to remove the one in main scene and continue to make

        _websocketClient = WebSocketClient.new()

        But no luck again, emit signal ends up wihout calling my method. But, anyway, I still try, thanks