0

I am trying to read from a network socket, To use the information in a gnome extension. For that I'm using the Gio socketclient. I'am able to connect and read synchrone from the inputstream. But I fail on the asynchrone reading. I've isolated my problem in the following script:

#!/usr/bin/gjs

const Gio = imports.gi.Gio;


var doAsyncCallBack = function(gobject, async_res, user_data) {
    print("ASYNC test: doCallBack");
    let lineout, charlength, error;
    [lineout, charlength, error] = gobject.read_upto_finish(async_res);
    print("ASYNC test: " + lineout + " (" + charlength + ")");
};


let sc = new Gio.SocketClient();
let scc = sc.connect_to_host("kodi", 9090, null);
let dis = new Gio.DataInputStream({ base_stream: scc.get_input_stream() });

//sync
print("sync test: started");
let valuestr, valueint;
[valuestr, valueint] = dis.read_upto("}" , -1,  null);
print("sync test: " + valuestr + " (" + valueint + ")");

//async
print("ASYNC test: started");
dis.read_upto_async("}" , -1,  0, null, doAsyncCallBack);

// keep in a loop.
print("(press [crtl-c] to end)");
while (0 == 0) {};

In my example the "doAsyncCallBack" function is never called. The synchrone call works, so the server is giving propper responses.

A part of the information I found here, but the (kodi) server isn't sending new-lines: gjs How to read a socket stream in Gnome Shell Extension using g_data_input_stream_read_line_async

EMoens
  • 1
  • 1

1 Answers1

0

I found out the reason why the callbacks don't work in my script. Basicly it was the last part of the code.

// while (0 == 0) {};
Clutter.init(null);
Clutter.main();

That was what I had to do, to make it work.

EMoens
  • 1
  • 1