简体   繁体   English

Gnome Javascript:回调不适用于异步功能

[英]Gnome Javascript: callback doesn't work for async functions

I am trying to read from a network socket, To use the information in a gnome extension. 我正在尝试从网络套接字读取信息,以在gnome扩展中使用该信息。 For that I'm using the Gio socketclient. 为此,我使用了Gio socketclient。 I'am able to connect and read synchrone from the inputstream. 我能够连接并从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. 在我的示例中,从未调用“ doAsyncCallBack”函数。 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 我在这里找到了一部分信息,但是(kodi)服务器没有发送换行符: gjs如何使用g_data_input_stream_read_line_async在Gnome Shell Extension中读取套接字流

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. 这就是我要做的,才能使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM