gjs如何使用g_data_input_stream_read_line_async在Gnome Shell Extension中读取套接字流 [英] gjs How to read a socket stream in Gnome Shell Extension using g_data_input_stream_read_line_async
问题描述
我正在尝试编写一个Gnome-Shell扩展,该扩展通过Socket Server与Arduino通信. Server和Arduino运行良好,但是我被扩展代码侦听Server传入的消息.
I'm trying to write a Gnome-Shell extension that communicates with Arduino through a Socket Server. The Server and Arduino are running fine, but I'm stuck at the extension code that listens for incoming Server messages.
由于我需要一种非阻塞方法,因此请使用 read_line_async 似乎很完美.
Since I need a non blocking approach, using read_line_async seems perfect.
但是我无法使其正常工作.这是我到目前为止(相关部分)的内容:
However I can't manage to get it to work. Here's what i got so far (relevant part):
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
}
异步功能可以很好地启动,并且当从服务器接收到一行时,也会调用_SocketRead
,但是它无法使用read_line_finish()
读取数据.
The async function is started just fine and also _SocketRead
gets called, when there's a line received from the server, but it fails to read the data with read_line_finish()
.
我对gio和Extension开发完全陌生,所以我可能会错过一些显而易见的东西.
I'm completely new to gio and Extension development so I might just miss something obvious.
在我看来,read_line_finish()
可能丢失了它的 GAsyncResult 参数,但是我不知道如何实现它.
To me it seems like read_line_finish()
may be missing it's GAsyncResult parameter, but i've got no clue on how to implement it.
回调函数和read_line_finish()缺少其参数. 多亏了Gerd的回答,我才得以使它发挥作用.帮助我找出说明"下 GIO参考中链接的示例.因此,这是用于比较的工作代码:
The Callback function and read_line_finish() were missing their parameters. Thanks to Gerd's answer I was able to make it work. Helped me to figure out the example linked in the GIO Reference under "Description". So here is the working code for comparison:
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
}
推荐答案
我对GJS还是陌生的,但是对编程语言的深刻理解使我想到了以下部分解决方案:根据
I am also new to GJS but a solid understanding of programming language led me to the following partial solution: According to the Gio DataStream reference You have to provide all required parameters to the method, e.g.,
let asyncResult = new Gio.SimpleAsyncResult();
let length;
let lineout = output_reader.read_line_finish(asyncResult, length);
HTH, 格尔德
HTH, Gerd
这篇关于gjs如何使用g_data_input_stream_read_line_async在Gnome Shell Extension中读取套接字流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!