Godot Websocket client communication with a python server

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

I want to make a multiplayer board game, I have a python server and the godot client, and somewhy the python server doesn’t notice the client trying to connect.

They python script never prints the “added connection…” and the client side never enters the connection_established signal and after a while it is closing with was_clean being false.

I tried changing the ip I connect to and it works

Client side:

var _client = WebSocketClient.new()
export var websocket_url = "127.0.0.1"

func _ready():
	_client.connect("connection_closed", self, "_closed")
	_client.connect("connection_error", self, "_closed")
	_client.connect("connection_established", self, "_connected")
	_client.connect("data_received", self, "_on_data")
	if _client.connect_to_url(websocket_url) != OK:
		print("Unable to connect")
		set_process(false)
	print('connecting')

func _closed(was_clean = false):
	print("Closed, clean: ", was_clean)
	set_process(false)

func _connected(proto = ""):
	main_board = preload("res://Prefabs/Board.tscn").instance()
	main_board.name = 'Board'
	add_child(main_board)
	main_board.scale = BOARD_SCALE
	main_board.generate_from_string("")

	enemy_board = preload("res://Prefabs/Board.tscn").instance()
	enemy_board.name = 'EnemeyBoard'
	add_child(enemy_board)
	enemy_board.scale = Vector2(1,1) - BOARD_SCALE
	enemy_board.position = Vector2(BOARD_SCALE.x * 64 * 6,0)
	enemy_board.generate_from_string(level)

Server side:

server_socket = socket()
server_socket.bind(('0.0.0.0', 80))
server_socket.listen(5)

game_lobby = []

while True:
    rlist, wlist, xlist = select([server_socket] + game_lobby, [], [])
    for current_socket in rlist:
        if current_socket == server_socket:
            (new_socket, address) = server_socket.accept()
            game_lobby.append(Player.copy(new_socket))
            print("Added connection: " + str(address))
        else:
            current_socket.close()
            game_lobby.remove(current_socket)
            print(f"Removed connection: {str(address)} Quit Lobby")
        if len(game_lobby) >= player_amount:
            game = Game(boards[randint(0,999)], game_lobby)
            game.start()
            game_lobby = []
:bust_in_silhouette: Reply From: Firty

I don’t know much about Python, but I have a lot of experience with websocket. Your client does not have a defined port (websocket_url = “127.0.0.1:5”). In this case, I noticed that your server is using Listen(5). The poll () function must be called constantly on the server and on the client, most people use it in _process. As I said I don’t know anything about python, so I don’t know how to anlizar this while function, but I believe that the poll() should be inside it. I didn’t understand this bind (“0.0.0.0”, 80)…
And lastly I recommend that you use a door with a higher value, for example 1122 or 9988. Doors below 100 can be used by some internal service of the system, I don’t know if it’s the case of 5 but for security, kick high kkkkk.

I tried it with 127.0.0.1:80 too and yet it didn’t work, The python script is working with the tcp peer on godot - that thing I know for sure but the problem is that I want to change my client side to websockets for convenience… So I think that the problem should be either in the client side or with something specific that relates to sockets on the server side.

Nahumus | 2021-04-01 20:33

and 5 isn’t my port, the parameter passed to the listen function of a socket in python is the maximum length of the queue I would like to have when the server is busy / when the code is blocked.

Nahumus | 2021-04-01 20:35

In Godot Listen(PORT) is the command that defines the port that the server will listen to.
Port 80 is http. It has to be a door like 1122.

Firty | 2021-04-02 00:29

Hi Firty,

Since you know a lot on Websocket. Do you know what is the equivalent for web socket with this UDP command like “while UDP.get_available_packet_count() > 0”?

kakcalu13 | 2022-04-04 13:23