Module jls.net.TcpClient
Provide TCP client socket class.
The network operations are only provided as asynchronous operations and thus should be used together with an event loop.
Usage:
local TcpClient = require('jls.net.TcpClient') local event = require('jls.lang.event') local client = TcpClient:new() client:connect('127.0.0.1', 8080):next(function(err) client:readStart(function(err, data) if data then print('Received "'..tostring(data)..'"') end client:readStop() client:close() end) end event:loop()
Class TcpClient
tcpClient:connect (addr, port, callback) | Connects this client to the specified address and port number. |
tcpClient:write (data, callback) | Writes data on this client. |
tcpClient:readStart (stream) | Starts reading data on this client. |
tcpClient:readStop () | Stops reading data on this client. |
tcpClient:setTcpNoDelay (on) | Enables or disables TCP no delay. |
tcpClient:setKeepAlive (on, delay) | Enables or disables keep alive. |
Class TcpClient
The TcpClient class.
A TCP Client allows to read and write on a stream connection.
- tcpClient:connect (addr, port, callback)
-
Connects this client to the specified address and port number.
Parameters:
- addr string the address, the address could be an IP address or a host name.
- port number the port number.
- callback function an optional callback function to use in place of promise.
Returns:
-
jls.lang.Promise
a promise that resolves once the client is connected.
Usage:
local s = TcpClient:new() s:connect('127.0.0.1', 80)
- tcpClient:write (data, callback)
-
Writes data on this client.
Parameters:
- data string the data to write.
- callback function an optional callback function to use in place of promise.
Returns:
-
jls.lang.Promise
a promise that resolves once the data has been written.
Usage:
local s = TcpClient:new() s:connect('127.0.0.1', 80):next(function() s:write('Hello') end)
- tcpClient:readStart (stream)
-
Starts reading data on this client.
Parameters:
- stream the stream reader, could be a function or a StreamHandler.
Usage:
local s = TcpClient:new() s:connect('127.0.0.1', 80):next(function() s:readStart(function(err, data) print('received', err, data) end) end)
- tcpClient:readStop ()
- Stops reading data on this client.
- tcpClient:setTcpNoDelay (on)
-
Enables or disables TCP no delay.
Parameters:
- on boolean true to activate TCP no delay.
- tcpClient:setKeepAlive (on, delay)
-
Enables or disables keep alive.
Parameters:
- on boolean true to activate keep alive.
- delay number the keep alive delay.