The websocket connection is disconnected after 6 seconds

I am using the following code to connect to the websocket, but the connection is disconnected after 6/5 seconds:

const Soup = imports.gi.Soup;
const GLib = imports.gi.GLib;
const byteArray = imports.byteArray;

const loop = GLib.MainLoop.new(null, false);

const session = Soup.Session.new();
const message = Soup.Message.new('GET', 'ws://127.0.0.1:9090');

session.websocket_connect_async(message, 'origin', [], null, null, websocket_connect_async_callback);

function websocket_connect_async_callback(_session, res) {
    let connection;

    try {
        connection = session.websocket_connect_finish(res);
    } catch (e) {
        logError(e);
        loop.quit();
        return;
    }

    connection.connect('closed', () => {
        log('closed');
        loop.quit();
    });

    connection.connect('error', (self, err) => {
        logError(err);
        loop.quit();
    });

    connection.connect('message', (self, type, data) => {
        if (type !== Soup.WebsocketDataType.TEXT)
            return;

        const str = byteArray.toString(byteArray.fromGBytes(data));
        log(`message: ${str}`);
    });

    log('open');
    connection.send_text(`      {
        "jsonrpc": "2.0",
        "method": "Configuration.Notifications",
        "params": {
          "player": true
        }
      }`);
}

loop.run();

It does not give any error or message when disconnected. It is the same when I disable the server

I’m not really a libsoup user, but isn’t that what the keepalive-interval property is for?

didn’t work.β€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œβ€Œ

Perhaps the connection is garbage-collected? Can you try putting let connection; in the global scope instead of the function scope?

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.