Reconnecting a WebSocketMultiplayerPeer

Godot Version

4.1.2

Question

I am working on a client/server project using the WebSocketMultiplayerPeer for communiaction. Creating the initial connection works fine and the signal handlers for peer_connected and peer_disconnected are called correctly when a client connects to the server.

In case of a connection loss, I want the client to automatically reconnect, by creating a new instance of the WebSocketMultiplayerPeer, connecting the signals, and calling create_client() with the server URL again.

I am checking if a reconnect is necessary in the _process() method (max once every 10 seconds), and the code for the new WebSocketMultiplayerPeer is called, but the connection just is not established.

For testing purposes I disabled the initial connection in the _ready() method and started with the reconnection flag set to true and in this case even the initial connection is not formed.

Both ways call the same method. Called from _ready() the connection works, but called from _process() it does not. This is the (simplyfied) method creating the client:

func _connect_to_server(recreate_connection: bool = false) :
	if recreate_connection:
		_socket = WebSocketMultiplayerPeer.new()
		_socket.peer_connected.connect(_on_socket_connected)
		_socket.peer_disconnected.connect(_on_socket_disconnected)

	print("Connecting to %s" % [_server_url])

	_socket.create_client(_server_url)

Does anybody have any ideas, why the connection is created correctly when called from _ready() but not from _process()?

Any hints or input on how to automatically reconnect a WebSocketMultiplayerPeer would be greatly appreciated!

Thanks in advance and best regards,
Patrick