0 votes

I'm working on a MMO game in Godot, but I'm stuck on solving an issue with websockets.

I made a server in Python a while ago, with the websockets package. I tested it with a JS client and everything worked correctly.

However, when I try to connect to it with Godot, the game can't connect to the server. There are no errors or anything, but client.get_connection_status() is set to 1 (CONNECTION_CONNECTING) and never changes.

The full code:

extends Node

var client = WebSocketClient.new()
var last_status = -1

func _ready():

    # Set events
    client.connect("connection_established", self, "started")
    client.connect("connection_error", self, "error", ["error"])
    client.connect("connection_closed", self, "error", ["closed"])
    client.connect("data_received", self, "read")
    set_process(true)

func _process(delta):

    var status = client.get_connection_status()

    if status != last_status:

        last_status = status
        print(status)

    if status == WebSocketClient.CONNECTION_DISCONNECTED: 

        return

    client.poll()

func start():

    # Connect to server
    print("connecting...")
    client.verify_ssl = false
    client.connect_to_url("ws://localhost:5678")

func started(protocol):

    print("success!")

func error(arg):

    print(": ", arg)

func read(pid=1):

    # Read message
    var packet = client.get_peer(1).get_packet()

    print("Received, %s" % [packet])

func write(data):

    # Send message
    client.get_peer(1).put_packet(data)

None of the signals ever gets triggered. The app produces the log:

** Debug Process Started **
OpenGL ES 3.0 Renderer: GeForce GT 640/PCIe/SSE2
0
connecting...
1

TL;DR There are no errors - the client just never ends connecting. What can be the reason of this? The server never receives any connections from Godot.

EDIT: I exported the project to HTML and uploaded it to my server. I'm getting the message Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received. StackOverflow tells me it's because the app requests with an unsupported subprotocol.

EDIT 2: I fixed the issue with subprotocols, but only for the HTML version. Doesn't work in other versions anyway.

Thanks!

in Engine by (38 points)
edited by

Looks like a bug - when I started the server on my domain, game successfully connected. localhost is probably not supported.

Edit: Now even connecting to my server isn't working.

1 Answer

+2 votes

There are 2 demos available here:

https://github.com/LudiDorici/godot-websocket

At least one of them will be added to the Godot-demo repo when websocket API will be locked for 3.1.

Regarding your code:

You must frequently poll the client via client.poll() (e.g. in _process) like explained in the docs

If you do not poll, HTML5 will work, native client not (because browsers do automatic polling)

by (56 points)
edited by

Also, if you are unsure if your python websocket server is working as expected try connecting to: ws://echo.websocket.org which is a public echo server you can use to test your client.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.