This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Im converting a Godot 3 game to Godot 4. The game has a Server script (c#) which uses a WebServerClient to communicate with a WebSocketServer.

WebSocketClient doesn't exist anymore in Godot 4, we now have WebSocketPeer. What I need is the Godot 4's equivalent of the following code:

WebSocketClient network = new WebSocketClient();
UTF8Encoding utf8 = new UTF8Encoding();

private void _DataReceived() {
    GD.Print(utf8.GetString(network.GetPeer(1).GetPacket()));
}
Godot version 4.0
in Engine by (149 points)

2 Answers

0 votes
Best answer

Theres been some changes to how Gd4's new WebSocketPeer works compared to Gd3's WebSocketClient. Here's my Gd3 implementation:

public override void _Ready() {
    GD.Print("Connecting");
    setup_connections();
    connect_to_server();
}

public override void _Process(float delta) {
    network.Poll();
}

private void _DataReceived() {
    deconstruct_message(utf8.GetString(network.GetPeer(1).GetPacket()));
}

private void setup_connections() {
    network.Connect("connection_established", this, "connection_succeeded");
    network.Connect("connection_closed", this, "server_disconnected");
    network.Connect("connection_error", this, "connection_failed");
    network.Connect("server_close_request", this, "forced_disconnect");
    network.Connect("data_received", this, "_DataReceived");
}

public void connect_to_server() {
    Error err = network.ConnectToUrl(url);

    if (err != Error.Ok) {
        GD.Print("Unable to Connect");
        SetProcess(false);
    }
}

To summarize it, I connect the WebSocketClient's "data_received" signal to the _DataReceived function to handle packets received there. However, this signal (as far as I can tell) was removed in Gd4. In order to tackle this problem, I just followed the steps in the WebSocketPeer documentation which for some reason I missed even though I visited the page a bunch of times! Here's the result:

public override void _Process(double delta) {
    network.Poll();

    WebSocketPeer.State state = network.GetReadyState();

    if (state == WebSocketPeer.State.Open) {
        while (network.GetAvailablePacketCount() > 0) {
            deconstruct_message(utf8.GetString(network.GetPacket()));
        }
    } else if (state == WebSocketPeer.State.Closed) {
        int code = network.GetCloseCode();
        string reason = network.GetCloseReason();
        GD.Print("Disconnected from server:   code:", code, "   reason:", reason);
        SetProcess(false);
    }
}
by (149 points)
+1 vote

While I haven't verified anything, a few observations:

  • In the 3.5 code posted above, network.GetPeer(1) returns a WebSocketPeer
  • In the 4.0 code, you're already working with a WebSocketPeer object

So, from that point forward, things should be, at least, similar...

Further, in the 3.5 code, you make a GetPacket call on the WebSocketPeer object. Looking, the 4.0 version of WebSocketPeer also has a GetPacket method.

So, it seems like the above should just require a comparison of the related docs and some minimal refactoring to get workigng in Godot 4.

by (22,704 points)

Turns out it was something along those lines, thanks for the help!

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 Frequently asked questions and 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 [email protected] with your username.