搜尋
首頁php教程php手册Swoole 初识 - wangyulu

官方定义:

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

 

简单案例:

<span style="color: #000000;">php

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Server
{

    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$serv</span><span style="color: #000000;">;

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct()
    {
        </span><span style="color: #800080;">$this</span>->serv = <span style="color: #0000ff;">new</span> swoole_server("0.0.0.0", 9501<span style="color: #000000;">);
        </span><span style="color: #800080;">$this</span>->serv->set(<span style="color: #0000ff;">array</span><span style="color: #000000;">(
            </span>'worker_num' => 8,
            'daemonize' => <span style="color: #0000ff;">false</span>,
            'max_request' => 10000,
            'dispatch_mode' => 2,
            'debug_mode' => 1<span style="color: #000000;">
        ));

        </span><span style="color: #800080;">$this</span>->serv->on('Start', <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>, 'onStart'<span style="color: #000000;">));
        </span><span style="color: #800080;">$this</span>->serv->on('Connect', <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>, 'onConnect'<span style="color: #000000;">));
        </span><span style="color: #800080;">$this</span>->serv->on('Receive', <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>, 'onReceive'<span style="color: #000000;">));
        </span><span style="color: #800080;">$this</span>->serv->on('Close', <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>, 'onClose'<span style="color: #000000;">));

        </span><span style="color: #800080;">$this</span>->serv-><span style="color: #000000;">start();
    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> onStart(<span style="color: #800080;">$serv</span><span style="color: #000000;">)
    {
        </span><span style="color: #0000ff;">echo</span> "Start\n"<span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> onConnect(<span style="color: #800080;">$serv</span>, <span style="color: #800080;">$fd</span>, <span style="color: #800080;">$from_id</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$serv</span>->send(<span style="color: #800080;">$fd</span>, "Hello {<span style="color: #800080;">$fd</span>}!"<span style="color: #000000;">);
    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> onReceive(swoole_server <span style="color: #800080;">$serv</span>, <span style="color: #800080;">$fd</span>, <span style="color: #800080;">$from_id</span>, <span style="color: #800080;">$data</span><span style="color: #000000;">)
    {
        </span><span style="color: #0000ff;">echo</span> "Get Message From Client {<span style="color: #800080;">$fd</span>}:{<span style="color: #800080;">$data</span>}\n"<span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> onClose(<span style="color: #800080;">$serv</span>, <span style="color: #800080;">$fd</span>, <span style="color: #800080;">$from_id</span><span style="color: #000000;">)
    {
        </span><span style="color: #0000ff;">echo</span> "Client {<span style="color: #800080;">$fd</span>} close connection\n"<span style="color: #000000;">;
    }

}

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 启动服务器</span>
<span style="color: #800080;">$server</span> = <span style="color: #0000ff;">new</span> Server();

 

<span style="color: #000000;">php

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Client
{

    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$client</span><span style="color: #000000;">;

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct()
    {
        </span><span style="color: #800080;">$this</span>->client = <span style="color: #0000ff;">new</span><span style="color: #000000;"> swoole_client(SWOOLE_SOCK_TCP);
    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> connect()
    {
        </span><span style="color: #0000ff;">if</span> (!<span style="color: #800080;">$this</span>->client->connect("127.0.0.1", 9501, 1<span style="color: #000000;">)) {
            </span><span style="color: #0000ff;">echo</span> "Error: {<span style="color: #800080;">$fp</span>->errMsg}[{<span style="color: #800080;">$fp</span>->errCode}]\n"<span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$message</span> = <span style="color: #800080;">$this</span>->client-><span style="color: #000000;">recv();
        </span><span style="color: #0000ff;">echo</span> "Get Message From Server:{<span style="color: #800080;">$message</span>}\n"<span style="color: #000000;">;

        </span><span style="color: #008080;">fwrite</span>(STDOUT, "请输入消息:"<span style="color: #000000;">);
        </span><span style="color: #800080;">$msg</span> = <span style="color: #008080;">trim</span>(<span style="color: #008080;">fgets</span><span style="color: #000000;">(STDIN));
        </span><span style="color: #800080;">$this</span>->client->send(<span style="color: #800080;">$msg</span><span style="color: #000000;">);
    }

}

</span><span style="color: #800080;">$client</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Client();
</span><span style="color: #800080;">$client</span>->connect();

 

分别打开两个终端输入:php server.php  php client.php 即可看到效果!

 

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

倉庫:如何復興隊友
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!