Websocket connection with C# not working

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

Hey,

I’m trying to connect to a Server on my local machine via Websocket. I’m using C# and my attempt to connect looks like this

using Godot;
using System;

public class WebsocketService {

    // private string URL = "ws://echo.websocket.org";
    private string URL = "ws://127.0.0.1:8080/ws-chat/123?name=Hostname";

    public WebsocketService() {
        Godot.WebSocketClient ws = new Godot.WebSocketClient();

        ws.ConnectToUrl(URL);
        GD.Print("Connected or not?? To " + URL);
        GD.Print(ws.GetConnectedHost());
    }

}

I get the following error

E 0:00:00.537   get_connected_host: Condition "!_peer->is_connected_to_host()" is true. Returned: IP_Address()
<C++ Source>  modules/websocket/wsl_client.cpp:318 @ get_connected_host()
<Stack Trace> :0 @ System.String Godot.NativeCalls.godot_icall_0_2(IntPtr , IntPtr )()
                WebSocketClient.cs:108 @ System.String Godot.WebSocketClient.GetConnectedHost()()
                WebsocketService.cs:18 @ WebsocketService..ctor()()
                Board.cs:17 @ Board..ctor()()

When using GDScript I have no problems to connect to my server via websocket.

Has anybody experience with this issue.

Thank you for your help

:bust_in_silhouette: Reply From: SirHeadless

So at the end I had to create my own WebsocketClient to connect to my server.

:bust_in_silhouette: Reply From: Dominik

Try:

private String URL = "ws://127.0.0.1:8080/ws-chat/123?name=Hostname";
private String[] supportedProtocols = new string[1]{"my-protocol"};
private Boolean multiplayer = false;
private Godot.WebSocketClient ws;

public override void _Ready()
{
    ws = new Godot.WebSocketClient();
    GD.Print(ws.ConnectToUrl(URL, supportedProtocols, multiplayer));
}