Object built-in object

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 TcpServerthe 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 Socketobject. We can use this object to send or receive TCP packets to and from the client.

The following is a TcpServerspecific 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 14
const 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 TcpServerobject and use the callback function onConnectto process the received client request information and write the data back to the client.

When this service is started, it will listen to 8080all IP addresses and requests on the port. When you telnetconnect 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

%0 object object toString() toJSON() TcpServer TcpServer new TcpServer() socket handler start() stop() object->TcpServer HttpServer HttpServer TcpServer->HttpServer SslServer SslServer TcpServer->SslServer HttpsServer HttpsServer HttpServer->HttpsServer

Constructor

TcpServer

TcpServer constructor, listen on all local addresses

1 2
new 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 3
new 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 2
new 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

1
readonly Socket TcpServer.socket;

handler

Handler, the current event processing interface object of the server

1
Handler TcpServer.handler;

member function

start

Start current server

1
TcpServer.start();

stop

Close the socket to abort the running server

1
TcpServer.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.

1
String 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.

1
Value TcpServer.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable