Module jls.net.TcpSocket

Represents a TCP socket.

The network operations are only provided as asynchronous operations and thus should be used together with an event loop.

Usage:

    local TcpSocket = require('jls.net.TcpSocket')
    local event = require('jls.lang.event')
    
    local client = TcpSocket: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 TcpSocket

tcpSocket:getLocalName () Returns the local name of this TCP socket.
tcpSocket:getRemoteName () Returns the remote name of this TCP socket.
tcpSocket:isClosed () Tells whether or not this TCP socket is closed.
tcpSocket:close ([callback]) Closes this TCP socket.
tcpSocket:connect (addr, port[, callback]) Connects this client to the specified address and port number.
tcpSocket:write (data[, callback]) Writes data on this client.
tcpSocket:readStart (sh) Starts reading data on this client.
tcpSocket:readStop () Stops reading data on this client.
tcpSocket:setTcpNoDelay (on) Enables or disables TCP no delay.
tcpSocket:setKeepAlive (on, delay) Enables or disables keep alive.
tcpSocket:bind (node, port[, backlog[, callback]]) Binds this server to the specified address and port number.
tcpSocket:onAccept (client) Accepts a new TCP client.


Class TcpSocket

The TcpSocket class. A TCP Client allows to read and write on a TCP connection. A TCP Server allows to listen and accept TCP connections.
tcpSocket:getLocalName ()
Returns the local name of this TCP socket.

Returns:

    string the local name of this TCP socket.
tcpSocket:getRemoteName ()
Returns the remote name of this TCP socket.

Returns:

    string the remote name of this TCP socket.
tcpSocket:isClosed ()
Tells whether or not this TCP socket is closed.

Returns:

    boolean true if the TCP socket is closed.
tcpSocket:close ([callback])
Closes this TCP socket.

Parameters:

  • callback function an optional callback function to use in place of promise. (optional)

Returns:

    jls.lang.Promise a promise that resolves once the socket is closed.
tcpSocket: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. (optional)

Returns:

    jls.lang.Promise a promise that resolves once the client is connected.

Usage:

    local s = TcpSocket:new()
    s:connect('127.0.0.1', 80)
tcpSocket:write (data[, callback])
Writes data on this client.

Parameters:

  • data string the data to write, could be a table of string.
  • callback function an optional callback function to use in place of promise. (optional)

Returns:

    jls.lang.Promise a promise that resolves once the data has been written.

Usage:

    local s = TcpSocket:new()
    s:connect('127.0.0.1', 80):next(function()
      s:write('Hello')
    end)
tcpSocket:readStart (sh)
Starts reading data on this client.

Parameters:

  • sh the stream handler, could be a function or a StreamHandler.

Usage:

    local s = TcpSocket:new()
    s:connect('127.0.0.1', 80):next(function()
      s:readStart(function(err, data)
        print('received', err, data)
      end)
    end)
tcpSocket:readStop ()
Stops reading data on this client.
tcpSocket:setTcpNoDelay (on)
Enables or disables TCP no delay.

Parameters:

  • on boolean true to activate TCP no delay.
tcpSocket:setKeepAlive (on, delay)
Enables or disables keep alive.

Parameters:

  • on boolean true to activate keep alive.
  • delay number the keep alive delay.
tcpSocket:bind (node, port[, backlog[, callback]])
Binds this server to the specified address and port number.

Parameters:

  • node string the address, the address could be an IP address or a host name.
  • port number the port number, 0 to let the system automatically choose a port.
  • backlog number the accept queue size, default is 32. (optional)
  • callback function an optional callback function to use in place of promise. (optional)

Returns:

    jls.lang.Promise a promise that resolves once the server is bound.

Usage:

    local s = TcpSocket:new()
    s:bind('127.0.0.1', 80)
tcpSocket:onAccept (client)
Accepts a new TCP client. This method should be overriden, the default implementation closes the client.

Parameters:

  • client the TCP client to accept.

Usage:

    local s = TcpSocket:new()
    function s:onAccept(client)
      client:close()
    end
generated by LDoc 1.4.6