Stuck at 101 switching protocols in Vala/Liboup websocket client

48 Views Asked by At

I am trying to connect to Revolt's (a chat app) websocket using the Vala example from Workbench, but it seems like I'm not receiving the expected ready event upon authenticating. Revolt docs:

After authenticating, the server will respond with Authenticated then it will send a Ready event containing useful data.

I check the http status code and I get 101 Switching Protocols.

I don't know how to proceed in this case. The Workbench example seems to work with the example websocket they provide.

Worth noting that I am new to Vala and the whole networking stack and I wasn't able to find online examples for Vala.

Here is the code I have so far:

class WebsocketClient : Object {
    private Soup.Session session;
    private Soup.WebsocketConnection connection;
        private string user_token;

    public WebsocketClient (Soup.Session global_session, string user_token) {
        this.session = global_session;
                this.user_token = user_token;

        Soup.Message request = new Soup.Message ("GET", @"wss://ws.revolt.chat?format=json&token=$(user_token)");

        session.websocket_connect_async.begin (request, null, null, 1, null, (obj, res) => {
            try {
                connection = session.websocket_connect_async.end (res);
            } catch (Error error_thing) {
                error (error_thing.message);
            }

            connection.error.connect (on_error);
            connection.closed.connect (on_closed);
            connection.message.connect (on_message);

                // on_open ();
        });
    }

        private void on_message (int type, Bytes msg) {
            if (type != Soup.WebsocketDataType.TEXT) return;

            string response = (string) msg.get_data ();
            message(response);
        }

        // other methods...

}

And I create the websocket in another file.

Given the explanation on valadoc, I'm expecting the switching to happen automatically or to handle it myself, but I can't find the Session.websocket_connect_finish method.

If the server returns "101 Switching Protocols", then msg's status code and response headers will be updated, and then the WebSocket handshake will be completed. On success, [[email protected]_connect_finish] will return a new [class@WebsocketConnection]. On failure it will return a Error.

1

There are 1 best solutions below

0
On BEST ANSWER

My silly goober self forgot about the async stuff. I had to move the connection logic inside a async method and call that method elsewhere.