Swoole 初識,swoole初識_PHP教程

來源:互聯網
上載者:User

Swoole 初識,swoole初識


官方定義:

Swoole:重新定義PHP

PHP的非同步、並行、高效能網路通訊引擎,使用純C語言編寫,提供了PHP語言的非同步多線程伺服器,非同步TCP/UDP網路用戶端,非同步MySQL,非同步Redis,資料庫連接池,AsyncTask,訊息佇列,毫秒定時器,非同步檔案讀寫,非同步DNS查詢。 Swoole內建了Http/WebSocket伺服器端/用戶端、Http2.0伺服器端。
Swoole可以廣泛應用於互連網、移動通訊、企業軟體、雲端運算、網路遊戲、物聯網、車連網、智能家居等領域。 使用PHP+Swoole作為網路通訊架構,可以使企業IT研發團隊的效率大大提升,更加專註於開發創新產品。

swoole 擴充安裝及案例來源:http://wiki.swoole.com/wiki/page/6.html

簡單案例:

phpclass 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();

phpclass 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();

分別開啟兩個終端輸入:php server.php  php client.php 即可看到效果!

http://www.bkjia.com/PHPjc/1116382.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1116382.htmlTechArticleSwoole 初識,swoole初識 官方定義: Swoole:重新定義PHP PHP的非同步、並行、高效能網路通訊引擎,使用純C語言編寫,提供了PHP語言的非同步多線...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.