Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Swoole base instance creation


May 14, 2021 Swoole



Build a basic instance of Swoole

Here's a basic swoole-based echo server

// Server
class Server
{
    private $serv;

    public function __construct() {
        $this->serv = new swoole_server("0.0.0.0", 9501);
        $this->serv->set(array(
            'worker_num' => 8,
            'daemonize' => false,
            'max_request' => 10000,
            'dispatch_mode' => 2,
            'debug_mode'=> 1
        ));

        $this->serv->on('Start', array($this, 'onStart'));
        $this->serv->on('Connect', array($this, 'onConnect'));
        $this->serv->on('Receive', array($this, 'onReceive'));
        $this->serv->on('Close', array($this, 'onClose'));

        $this->serv->start();
    }

    public function onStart( $serv ) {
        echo "Start\n";
    }

    public function onConnect( $serv, $fd, $from_id ) {
        $serv->send( $fd, "Hello {$fd}!" );
    }

    public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
        echo "Get Message From Client {$fd}:{$data}\n";
    }

    public function onClose( $serv, $fd, $from_id ) {
        echo "Client {$fd} close connection\n";
    }
}
// 启动服务器
$server = new Server();

As you can see from the code, creating swoole_server basically three steps: 1. C reate a new object swoole_server constructor 2. C all set function settings swoole_server configuration options 3. Call the on function to set the relevant callback function For specific instructions on set configuration options and on callback functions, refer to the swoole documentation I've organized (configuration options). )

Here's just a brief introduction. T he onStart callback is called before the server runs, onConnect is called when a new client connects, the onReceive function is called when data is sent to the server, and onClose is called when a client is disconnected. H ere you can probably see how to use swoole: listening to new connections at onConnect; R eceive the data at onReceive and process it, and then you can call the send function to send the processing results; Handle client offline events at onClose.

Here's the code for the client:

<?php
class Client
{
    private $client;

    public function __construct() {
        $this->client = new swoole_client(SWOOLE_SOCK_TCP);
    }

    public function connect() {
        if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
            echo "Error: {$fp->errMsg}[{$fp->errCode}]\n";
        }
        $message = $this->client->recv();
        echo "Get Message From Server:{$message}\n";

        fwrite(STDOUT, "请输入消息:");  
        $msg = trim(fgets(STDIN));
        $this->client->send( $msg );
    }
}

$client = new Client();
$client->connect();

Here, swoole_client TCP-based client instance and call the connect function to initiate a connection request to the specified IP and port. R equests can then be received and sent through the recv() and send() functions. It is important to note that I use the default sync blocking client here, so both the recv and send operations can cause network blocking.