ObjectTcpServer
TcpServer` is a high-concurrency TCPSocketServer can be used to create a TCP server that has established a TCP connection with the client in the initial state.
Use TcpServer
the object to quickly create a multi-fiber concurrent processing TCP server. When a client connects to the listening address, the callback function will be called and return a new connected Socket
object. We can use this object to send or receive TCP packets to and from the client.
The following is a TcpServer
specific example of writing back a client TCP message based on an object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14const net = require("net");
function onConnect(conn) {
console.log(`new client accepted! local:${conn.localAddress}, remote:${conn.remoteAddress}`);
const data = conn.read();
if (data) {
console.log(`recv data on fn onConnect: ${data}`);
conn.write(data);
}
conn.close();
}
new net.TcpServer('0.0.0.0', 8080, onConnect).start();
console.log('server is running on port: 8080');
In the above code, we create an TcpServer
object and use the callback function onConnect
to process the received client request information and write the data back to the client.
When this service is started, it will listen to 8080
all IP addresses and requests on the port. When you telnet
connect to the service through or other client tools, you will see the service print connection information and send every request you send. The request is sent back as is.
inheritance relationship
Constructor
TcpServer
TcpServer constructor, listen on all local addresses
1
2new TcpServer(Integer port,
Handler listener);
Call parameters:
- port: Integer, specify the tcp server listening port
- listener:Handler, specify the built-in message processor, processing function, chain processing array, and routing object received by tcp. For details, seemq.Handler
TcpServer constructor
1
2
3new TcpServer(String addr,
Integer port,
Handler listener);
Call parameters:
- addr: String, specifies the listening address of the tcp server. If it is "", it will listen on all addresses of the local machine.
- port: Integer, specify the tcp server listening port
- listener:Handler, specify the built-in message processor, processing function, chain processing array, and routing object of the connection received by tcp. For details, seemq.Handler
TcpServer constructor
1
2new TcpServer(String addr,
Handler listener);
Call parameters:
- addr: String, specifies the unix socket or Windows pipe server listening address
- listener:Handler, specify the built-in message processor, processing function, chain processing array, and routing object of the connection received by tcp. For details, seemq.Handler
member properties
socket
Socket, the server is currently listening toSocketobject
1readonly Socket TcpServer.socket;
handler
Handler, the current event processing interface object of the server
1Handler TcpServer.handler;
member function
start
Start current server
1TcpServer.start();
stop
Close the socket to abort the running server
1TcpServer.stop() async;
toString
Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.
1String TcpServer.toString();
Return results:
- String, returns the string representation of the object
toJSON
Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.
1Value TcpServer.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable