server.php #A TCP server is created here to listen to the local port 9501. Its logic is very simple. When the client Socket sends a hello string through the network, the server will reply with a Server: hello string.
Server is an asynchronous server, so the program is written by listening to events. When the corresponding event occurs, the underlying layer will actively call back the specified function. For example, when a new TCP connection comes in, the onConnect event callback will be executed. When a connection sends data to the server, the onReceive function will be called back.UDP server is different from TCP server. UDP has no concept of connection.
After starting the Server, the client can directly send data packets to the 9502 port monitored by the Server without Connect. The corresponding event is onPacket. $clientInfo is client-related information, an array, including the client’s IP and port, etc. Call the $server->sendto method to send data to the clientHttp server You only need to pay attention to the request response, so you only need to listen to one onRequest event.
This event will be triggered when a new HTTP request comes in. The event callback function has two parameters. One is the $request object, which contains request-related information, such as GET/POST request data. The other is the response object. The response to the request can be completed by operating the response object. The $response->end() method means to output a piece of HTML content and end the request.0.0.0.0 means monitoring all IP addresses. A server may have multiple IPs at the same time, such as 127.0.0.1 local loopback IP, 192.168.1.100 LAN IP, 210.127.20.2 external network IP, which can also be used separately Specify a listening port for IP9501. If it is occupied, the program will throw a fatal error and interrupt execution.
The WebSocket server is a long-connection server built on the Http server. The client will first send an Http request to shake hands with the server.
After the handshake is successful, the onOpen event will be triggered, indicating that the connection is ready. The $request object can be obtained in the onOpen function, which contains information related to the Http handshake, such as GET parameters, Cookie, Http header information, etc.After the connection is established, the client and server can communicate in two directions.
When the client sends information to the server, the server triggers the onMessage event callback. The server can call $server->push() to send a message to a client (using the $fd identifier). The server can Set up the onHandShake event callback to handle the WebSocket handshake manually. swoole_http_server is a subclass of swoole_server, with built-in support for Http. swoole_websocket_server is a subclass of swoole_http_server, with built-in support for WebSocketThe above is the detailed content of Differences between various swoole servers. For more information, please follow other related articles on the PHP Chinese website!