如何使用swoole快速搭建echo server


關於在linux中,如何搭建環境然后加載swoole擴展,本文不論述。


1.創建Server.php和Client

在htdocs文件夾下創建Server.php(當然如果你自定義了路徑,就另當別論了)

cd /usr/lcoal/httpd/htdocs

vi Server.php

這里使用easyswoole文檔中的范例程序

https://linkeddestiny.gitbooks.io/easy-swoole/content/book/chapter01/echo_server.html

// 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,
        ));

        $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";
        $serv->send($fd, $data);
    }

    public function onClose( $serv, $fd, $from_id ) {
        echo "Client {$fd} close connection\n";
    }
}
// 啟動服務器 Start the server
$server = new Server();

 

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: {$this->client->errMsg}[{$this->client->errCode}]\n";
        }

        fwrite(STDOUT, "請輸入消息 Please input msg:");  
        $msg = trim(fgets(STDIN));
        $this->client->send( $msg );

        $message = $this->client->recv();
        echo "Get Message From Server:{$message}\n";
    }
}

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

 


 

 

2.使用Server.php

php /usr/local/httpd/htdocs/Server.php

如果出現錯誤,說明你已經開啟了端口,查看端口關閉,在執行就行了

[2019-04-23 14:55:24 @10601.0] WARNING swSocket_bind(:428): bind(0.0.0.0:9501) failed, Error: Address already in use[98]
PHP Fatal error: Uncaught Swoole\Exception: failed to listen server port[0.0.0.0:9501], Error: Address already in use[98] in /usr/local/httpd/htdocs/Server.php:8
Stack trace:
#0 /usr/local/httpd/htdocs/Server.php(8): Swoole\Server->__construct('0.0.0.0', 9501)
#1 /usr/local/httpd/htdocs/Server.php(40): Server->__construct()
#2 {main}
thrown in /usr/local/httpd/htdocs/Server.php on line 8

Fatal error: Uncaught Swoole\Exception: failed to listen server port[0.0.0.0:9501], Error: Address already in use[98] in /usr/local/httpd/htdocs/Server.php:8
Stack trace:
#0 /usr/local/httpd/htdocs/Server.php(8): Swoole\Server->__construct('0.0.0.0', 9501)
#1 /usr/local/httpd/htdocs/Server.php(40): Server->__construct()
#2 {main}
thrown in /usr/local/httpd/htdocs/Server.php on line 8

 

netstat -anp|grep 9501

kill -9 +上一步獲取的端口    如:kill -9 9719

 

3.開啟客戶端Client.php

開啟一個客戶端,輸入

php /usr/local/httpd/htdocs/Client.php

 

4.測試

在客戶端輸入內容

服務端顯示如下:

Get Message From Client 1:156165
[2019-04-23 14:34:58 *10389.7] NOTICE swFactoryProcess_finish (ERRNO 1004): send 6 byte failed, because connection[fd=1] is closed
Client 1 close connection

echo server快速搭建成功。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM