Rumah  >  Artikel  >  php教程  >  Swoole 初识 - wangyulu

Swoole 初识 - wangyulu

WBOY
WBOYasal
2016-05-20 11:54:331250semak imbas

官方定义:

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 即可看到效果!

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn