我正在尝试编写一个通过套接字服务器与Arduino通信的Gnome-Shell扩展。服务器和Arduino运行正常,但是我仍然停留在侦听传入服务器消息的扩展代码中。
由于我需要一种非阻塞方法,因此使用read_line_async似乎很完美。
然而,我无法让它发挥作用。这是我到目前为止(相关部分):
let sockClient, sockConnection, output_reader, receivedline;
// connect to socket
sockClient = new Gio.SocketClient();
sockConnection = sockClient.connect_to_host("127.0.0.1:21567", null, null);
// read server socket
output_reader = new Gio.DataInputStream({ base_stream: sockConnection.get_input_stream() });
output_reader.read_line_async(0, null, _SocketRead, null);
// callback
function _SocketRead() {
let [lineout, charlength, error] = output_reader.read_line_finish();
receivedline = lineout;
// process received data
}
async函数启动得很好,当从服务器收到一行时,也会调用_SocketRead
,但它无法用read_line_finish()
读取数据。
我对gio和Extension开发完全不熟悉所以我可能会错过一些明显的东西。
对我而言,似乎read_line_finish()
可能缺少GAsyncResult参数,但我对如何实现它没有任何线索。
编辑:
Callback函数和read_line_finish()缺少参数。 感谢Gerd的回答,我能够让它发挥作用。帮助我弄清楚“描述”下GIO Reference中链接的示例。所以这是用于比较的工作代码:
let sockClient, sockConnection, output_reader, receivedline;
// connect to socket
sockClient = new Gio.SocketClient();
sockConnection = sockClient.connect_to_host("127.0.0.1:21567", null, null);
// read server socket
output_reader = new Gio.DataInputStream({ base_stream: sockConnection.get_input_stream() });
output_reader.read_line_async(0, null, _SocketRead, null);
// callback
function _SocketRead(gobject, async_res, user_data) {
let [lineout, charlength, error] = gobject.read_line_finish(async_res);
receivedline = lineout;
// process received data
}
答案 0 :(得分:2)
我也是GJS的新手,但对编程语言的深刻理解使我得到了以下部分解决方案:根据the Gio DataStream reference你必须为方法提供所有必需的参数,例如,
let asyncResult = new Gio.SimpleAsyncResult();
let length;
let lineout = output_reader.read_line_finish(asyncResult, length);
HTH, 格尔德