2017-02-18 27 views
0

我想從網絡套接字中讀取,以使用gnome擴展中的信息。爲此我使用Gio socketclient。我可以連接並從輸入流中讀取synchrone。但是,我在閱讀asynchrone時失敗了。我已經在以下腳本中隔離了我的問題:Gnome Javascript:回調不適用於異步功能

#!/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) {}; 

在我的示例中,「doAsyncCallBack」函數從不調用。 synchrone調用起作用,所以服務器正在給予propper響應。

的我發現這裏的信息的一部分,但(科迪)服務器不發送的新行:gjs How to read a socket stream in Gnome Shell Extension using g_data_input_stream_read_line_async

回答

0

我找到了原因回調並不在我的劇本工作的原因。基本上它是代碼的最後一部分。

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

這就是我所要做的,使它工作。