I tried to create a listening unix domain socket.
I have two functions.
The first one called create_unixSocket_listener creates a socket over Gio.Socket, binds to the unix socket address, listens, and accept an incoming remote_socket.
The second one called create_socketListener creates a Gio.SocketListener instance, adds the unix socket address, and accept an incoming connection
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
const loop = new GLib.MainLoop(null, false),
pwd= GLib.get_current_dir();
let socket;
function create_unixSocket_listener (fileName)
{
let socket_address, remote_socket, file;
socket=Gio.Socket.new (Gio.SocketFamily.UNIX, Gio.SocketType.STREAM, Gio.SocketProtocol.DEFAULT);
socket.init(null);
socket_address=Gio.UnixSocketAddress.new (pwd+"/"+fileName);
file=Gio.File.new_for_path (socket_address.get_path());
if (file.query_exists (null)) file.delete (null);
socket.set_blocking (true);
socket.set_timeout (0);
socket.set_keepalive(true);
socket.bind (socket_address, true);
socket.listen ();
remote_socket=socket.accept (null);
log (remote_socket);
}
let socketListener;
function create_socketListener (fileName)
{
let socket_address, connection, file;
socketListener=Gio.SocketListener.new ();
socket_address=Gio.UnixSocketAddress.new (pwd+"/"+fileName);
file=Gio.File.new_for_path (socket_address.get_path());
if (file.query_exists (null)) file.delete (null);
socketListener.add_address (socket_address, Gio.SocketType.STREAM, Gio.SocketProtocol.DEFAULT, null);
connection=socketListener.accept (null);
log (connection);
}
create_socketListener ("unixSocket");
await loop.runAsync();
When i start create_socketListener the "unixSocket" file is created, and the socket is listening for an incoming connection.
When i open terminal and enter nc -U unixSocket
, then an error shows up
JS ERROR: TypeError: value is null
The return value of the instance method socketListener.accept is null.
I cant retrieve any connection
When i use create_unixSocket_listener and open a terminal with nc -U unixSocket
, i get the remote_socket --> success, but then after 5 seconds nc is closing, altough, the socket variable is global, and i set keepalive, and timeout to 0..
So both functions are useless, seems the whole Gio Socket class and the other classes which are for socket connections, do not work in GJS. Is that right?
on the remote_socket, which i get back with success, i cant use the receive function and create_source function. I have to use a UnixInputStream and set the file descriptor manually, to receive or write anything back, but after 5 seconds nc closes. -why?
if socketListener doesnt work, i cant use Gio.UnixConnection and i still dont know, if im able to use Gio.TlsConnection for my manually set up unix input and output streams
Do you have working examples?
Question is answered by andy holmes. see comments