node_socket_client
Low level bindings to Node’s net Socket client.
https://nodejs.org/api/net.html#class-netsocket
Types
pub type ConnectionFamily {
Ipv6
Ipv4
}
Constructors
-
Ipv6
-
Ipv4
pub type Event(data) {
CloseEvent(had_error: Bool)
ConnectEvent
ConnectionAttemptEvent(
ip: String,
port: Int,
family: ConnectionFamily,
)
ConnectionAttemptFailedEvent(
ip: String,
port: Int,
family: ConnectionFamily,
error: String,
)
ConnectionAttemptTimeoutEvent(
ip: String,
port: Int,
family: ConnectionFamily,
)
DataEvent(data: data)
DrainEvent
EndEvent
ErrorEvent(error: String)
LookupEvent(
result: Result(Nil, String),
address: String,
family: Option(ConnectionFamily),
host: String,
)
ReadyEvent
TimeoutEvent
}
Constructors
-
CloseEvent(had_error: Bool)
Emitted once the socket is fully closed.Bool
The argument
had_error
is a bool which says if the socket was closed due to a transmission error.https://nodejs.org/api/net.html#event-close_1
-
ConnectEvent
Emitted when a socket connection is successfully established.
https://nodejs.org/api/net.html#event-connect
-
ConnectionAttemptEvent( ip: String, port: Int, family: ConnectionFamily, )
Emitted when a new connection attempt is started. This may be emitted multiple times if the family autoselection algorithm is enabled.
https://nodejs.org/api/net.html#event-connectionattempt
-
ConnectionAttemptFailedEvent( ip: String, port: Int, family: ConnectionFamily, error: String, )
Emitted when a connection attempt failed. This may be emitted multiple times if the family autoselection algorithm is enabled.
https://nodejs.org/api/net.html#event-connectionattemptfailed
-
ConnectionAttemptTimeoutEvent( ip: String, port: Int, family: ConnectionFamily, )
Emitted when a connection attempt timed out. This is only emitted (and may be emitted multiple times) if the family autoselection algorithm is enabled.
https://nodejs.org/api/net.html#event-connectionattempttimeout
-
DataEvent(data: data)
Emitted when data is received.
https://nodejs.org/api/net.html#event-data
-
DrainEvent
Emitted when the write buffer becomes empty. Can be used to throttle uploads.
https://nodejs.org/api/net.html#event-drain
-
EndEvent
Emitted when the other end of the socket signals the end of transmission, thus ending the readable side of the socket.
https://nodejs.org/api/net.html#event-end
-
ErrorEvent(error: String)
Emitted when an error occurs. The ‘close’ event will be called directly following this event.
https://nodejs.org/api/net.html#event-error_1
-
LookupEvent( result: Result(Nil, String), address: String, family: Option(ConnectionFamily), host: String, )
Emitted when an error occurs. The ‘close’ event will be called directly following this event.
https://nodejs.org/api/net.html#event-lookup
-
ReadyEvent
Emitted when a socket is ready to be used.
Triggered immediately after ‘connect’.
https://nodejs.org/api/net.html#event-ready
-
TimeoutEvent
Emitted if the socket times out from inactivity. This is only to notify that the socket has been idle. The user must manually close the connection.
https://nodejs.org/api/net.html#event-timeout
https://nodejs.org/api/net.html#class-netsocket
pub type SocketClient
Functions
pub fn connect(
host host: String,
port port: Int,
state state: a,
handler handler: fn(a, SocketClient, Event(String)) -> a,
) -> SocketClient
Create a UTF-8 encoded socket connection to a given host and port.
pub fn destroy(socket: SocketClient) -> Nil
Close the socket forcefully, even if there is data in the buffer still to be written.
If the ‘finish’ event was already emitted the socket is destroyed immediately. If the socket is still writable it implicitly calls end().
pub fn destroy_soon(socket: SocketClient) -> Nil
Close the socket forcefully, once all bufferd data has been written to it.
If the ‘finish’ event was already emitted the socket is destroyed immediately. If the socket is still writable it implicitly calls end().
pub fn end(socket: SocketClient) -> Nil
Close the socket gracefully.
This will “half-close” the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.
pub fn write(socket: SocketClient, data: String) -> Bool
Sends data on the socket with UTF-8 encoding.
Returns true if the entire data was flushed successfully to the kernel buffer. Returns false if all or part of the data was queued in user memory. ‘drain’ will be emitted when the buffer is again free.