The following shows net module APIs available for each platform.
| Linux (Ubuntu) |
Raspbian (Raspberry Pi) |
Nuttx (STM32F4-Discovery) |
|
|---|---|---|---|
| net.createServer | O | O | O |
| net.connect | O | O | O |
| net.createConnection | O | O | O |
| net.Server.listen | O | O | O |
| net.Server.close | O | O | O |
| net.Socket.connect | O | O | O |
| net.Socket.write | O | O | O |
| net.Socket.end | O | O | O |
| net.Socket.destroy | O | O | O |
| net.Socket.pause | O | O | O |
| net.Socket.resume | O | O | O |
| net.Socket.setTimeout | O | O | O |
| net.Socket.setKeepAlive | X | X | X |
※ When writable stream is finished but readable stream is still alive, IoT.js tries to shutdown the socket, not destroy.
However on nuttx due to lack of implementation, it does nothing inside.
IoT.js provides asynchronous networking through Net module.
You can use this module with require('net') and create both servers and clients.
options: Objectport: Numberhost: String, Default:localhostconnectListener: Function()
Creates a net.Socket and connects to the supplied host.
It is equivalent to new net.Socket() followed by socket.connect().
options: ObjectconnectionListener: Function(connection: net.Socket)
Creates a TCP server according to options.
connectionListener is automatically registered as connection event listener.
You can create net.Server instance with net.createServer().
callback: Function()
Emitted when server closed.
Note that this event will be emitted after all existing connections are closed.
callback: Function(socket)socket: net.Socket
Emitted when new connection is established.
callback: Function()
Emitted when an error occurs.
callback: Function()
Emitted when server has been started listening.
closeListener: Function()
Stops listening new arriving connection.
Server socket will finally close when all existing connections are closed, then emit 'close' event.
closeListener is registered as close event listener.
port: Numberhost: Stringbacklog: NumberlistenListener: Function()
Starts listening and accepting connections on specified port and host.
net.Socket inherits Stream.Duplex
options: Object
Creates a new socket object.
options is an object specifying following information:
allowHalfOpen: Boolean
callback: Function()
Emitted after connection is established.
callback: Function()
Emitted when the socket closed.
callback: Function(data)data: Buffer | String
Emitted when data is received from the connection.
callback: Function()
Emitted when the write buffer becomes empty.
callback: Function()
Emitted when FIN packet received.
callback: Function()
Emitted when an error occurs.
callback: Function(err, address, family)err: Error | Nulladdress: Stringfamily: String | Null
Emitted after resolving hostname.
callback: Function()
Emitted when the connection remains idle for specified timeout.
options: Objectport: Number- 'host: String
, Default:'localhost'`
Opens the connection with supplied port and host.
options is an object specifying following information:
port: Number- port connect to (required)host: String- host connect to (optional, default:'127.0.0.1')
connectionListener is automatically registered as connect event listener which will be emitted when the connection is established.
Destroys the socket.
data: String | Buffercallback: Function()
Half-closes the socket.
If data is given it is equivalent to socket.write(data) followed by socket.end().
data: String | Buffer
Pauses reading data.
Resumes reading data after a call to pause().
enable: BooleaninitialDelay: Number, Default:0
Enables or disables keep-alive functionality.
timeout: Numbercallback: Function()
Sets timeout for the socket.
If the socket is inactive for timeout milliseconds, 'timeout' event will emit.
callback is registered as timeout event listener.
data: String | Buffercallback: Function()
Sends data on the socket.
callback function will be called after given data is flushed through the connection.