Table of Contents
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();
Copy after login
<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();
Copy after login
分别打开两个终端输入:php server.php php client.php 即可看到效果!
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
4 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
4 weeks ago
By DDD
Two Point Museum: All Exhibits And Where To Find Them
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
