Godot version:
The bug occurs in 0b6b49a897b35bec53765e1288c32d57afa1a293
The bug does not occur in the stable version.
Issue description:
When using this code in _ready():
var socket = WebSocketClient.new()
socket.connect("data_received", self, "on_data_received")
the on_data_received callback will never be called
Maybe this relates to the latest changes regarding the websocket library?
Edit: added example projects
Godot - WebsocketBug.zip
Python Server - bugserver.zip
Can you provide a minimal reproduction project?
Having those 2 lines in _ready will cause socket to go out of scope after the _ready so it surely won't work.
This works though:
extends Control
var socket = null
func _ready():
socket = WebSocketClient.new()
socket.connect_to_url("ws://echo.websocket.org")
socket.connect("connection_established", self, "conn")
socket.connect("data_received", self, "on_data_received")
func conn(proto):
socket.get_peer(1).put_packet("asd".to_utf8())
func _process(delta):
socket.poll()
pass
func on_data_received():
print("recv")
When I create a small project I can not reproduce the bug, even when using my own server.
But I have a bigger project that works fine in stable but does not work with the latest code when building godot from master.
What I noticed there, when I connect to the socket-signal connection_established and print the parameter for this function it is "default" for all the working projects and in the non-working project it is an empty string.
func _ready():
...
socket.connect("connection_established", self, "on_connection_established")
func on_connection_established(proto):
print(proto)
I am using exactly the same code in Godot and for the server.
Is this of any help?
But I have a bigger project that works fine in stable but does not work with the latest code when building godot from master.
Are you getting any error in the debugger errors tab?
What I noticed there, when I connect to the socket-signal
connection_establishedand print the parameter for this function it is"default"for all the working projects and in the non-working project it is an empty string.
Yeah, that's on purpose, when not specifying a sub-protocol and the connection succeeded the parameter used to be default due to limitation of the library we were using.
We now correctly set it to an empty string. This does not change anything else unless your game have custom logic on that parameter and expects it to be default.
So I just spent some time and figured something out. The call that does not work is called on the server immediately when the websocket registers.
I added two example zips to my original comment.
The server is written in python (3.6).
The godot project can be start and in the output you will see when the connection has been established and when data is received.
It should look like this (and it does in stable)
Connection established.
Data received:
{connection_id:a2537f3ac8d2418baf5462a4b00be086, type:connection_id}
In the current master the connection is established but it does not receive the data.
I hope you can run the python server to see the problem yourself.
@BeayemX thank you so much for spotting this and taking the time for making a reproduction project.
I opened a PR with the fix. :confetti_ball:
Most helpful comment
So I just spent some time and figured something out. The call that does not work is called on the server immediately when the websocket registers.
I added two example zips to my original comment.
The server is written in python (3.6).
The godot project can be start and in the output you will see when the connection has been established and when data is received.
It should look like this (and it does in stable)
In the current master the connection is established but it does not receive the data.
I hope you can run the python server to see the problem yourself.