Module for communicating over raw TCP, UDP and Unix domain sockets.
hs.socket.create() makes a stream socket, which can connect to a TCP server or Unix domain
socket (optionally over TLS), or listen for incoming connections. hs.socket.createUDP()
makes a UDP socket, which can send and receive datagrams, including broadcasts.
hs.socket.createUnixDatagram() makes a Unix domain datagram socket, which exchanges
datagrams with other programs on this Mac through a socket file.
Sockets report everything through a single callback, set with setCallback(). Received data
is delivered as a Uint8Array; hs.socket.decodeText() turns it into a string.
TCP client
hs.socket.create().setCallback((event, sock, data) => { if (event === "connected") sock.write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n").readUntil("\r\n\r\n") if (event === "data") console.log(hs.socket.decodeText(data)) if (event === "error") console.error(data) }).connect("example.com", 80)
TCP server
Each connection a listening socket accepts arrives as a new socket, with the same callback.
Module for communicating over raw TCP, UDP and Unix domain sockets.
hs.socket.create()makes a stream socket, which can connect to a TCP server or Unix domain socket (optionally over TLS), or listen for incoming connections.hs.socket.createUDP()makes a UDP socket, which can send and receive datagrams, including broadcasts.hs.socket.createUnixDatagram()makes a Unix domain datagram socket, which exchanges datagrams with other programs on this Mac through a socket file.Sockets report everything through a single callback, set with
setCallback(). Received data is delivered as aUint8Array;hs.socket.decodeText()turns it into a string.TCP client
TCP server
Each connection a listening socket accepts arrives as a new socket, with the same callback.
UDP
Unix domain datagrams
For example, to receive messages from Karabiner-Elements'
send_user_command: