Home > php教程 > php手册 > Swoole 初识,swoole初识

Swoole 初识,swoole初识

WBOY
Release: 2016-06-13 08:42:57
Original
1082 people have browsed it

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

 

简单案例:

<?<span>php

</span><span>class</span><span> Server
{

    </span><span>private</span> <span>$serv</span><span>;

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

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

        </span><span>$this</span>->serv-><span>start();
    }

    </span><span>public</span> <span>function</span> onStart(<span>$serv</span><span>)
    {
        </span><span>echo</span> "Start\n"<span>;
    }

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

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

    </span><span>public</span> <span>function</span> onClose(<span>$serv</span>, <span>$fd</span>, <span>$from_id</span><span>)
    {
        </span><span>echo</span> "Client {<span>$fd</span>} close connection\n"<span>;
    }

}

</span><span>//</span><span> 启动服务器</span>
<span>$server</span> = <span>new</span> Server();
Copy after login

<?<span>php

</span><span>class</span><span> Client
{

    </span><span>private</span> <span>$client</span><span>;

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

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

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

}

</span><span>$client</span> = <span>new</span><span> Client();
</span><span>$client</span>->connect();
Copy after login

 

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

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template