0 votes

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 connectionestablished signal and after a while it is closing with wasclean 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 = []
Godot version 3.2.1
in Engine by (27 points)

1 Answer

0 votes

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.

by (211 points)

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.

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.

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.

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.