Connecting to websockets doesn't end.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Soaku

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!

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.

Soaku | 2018-09-23 13:21

:bust_in_silhouette: Reply From: Fales

There are 2 demos available here:

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)

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.

Fales | 2018-09-24 16:03