[Swoole] Ubuntuのインストールでは、ファストスタート

本論文では、UbuntuのインストールSwooleでコンパイラを記述し、テストおよび取り扱いを含む文書、与えられた公式のデモによると:で、TCPサーバ、UDPサーバー、HTTPサーバー、WebSocketのサーバー、非同期クライアント、タイマーとの共同範囲の相関を公式には、新しいプログラミングモデルやアイデアを持ってSwooleを楽しむPHPer例を模倣します。

これは、PHPのWebプログラミングの不備を補います。

I.説明

動作環境:win10UbuntuPHP7.2Swoole4.3

参照文献:https://wiki.swoole.com/wiki/page/p-quickstart.html

第二に、インストールSwoole

  • ダウンロードエキス
sudo wget https://github.com/swoole/swoole-src/archive/v4.3.6.tar.gz
cp v4.3.6.tar.gz swoole-v4.3.6.tar.gz
tar -zxvf swoole-v4.3.6.tar.gz
cd swoole-v4.3.6
  • インストールが依存します
# 根据下面的编译提示进行选择安装

sudo apt-get install php-dev
sudo apt-get install autoconf
  • コンパイルとインストール
# 根据自己 php 安装的目录
cd swoole-v4.3.6
/usr/local/php/bin/phpize
./configure
make
sudo make install

make 結果:

...
----------------------------------------------------------------------
Libraries have been installed in:
   /home/fly/swoole-src-4.3.6/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

sudo make install 結果:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/
Installing header files:          /usr/local/php/include/php/
  • php.ini設定
# 编译安装成功后,在 php.ini 文件加入
extension=swoole.so

# extension=/usr/lib/php/20170718/swoole.so

負荷かどうかを確認しswoole拡張

php -m
  • エラー調査(無視できます)

次のエラーが明らかに、次のように指定する必要がありますphp-confパスを:

設定:エラー:PHP-config設定を見つけることができません。--with-PHP-config設定= PATHを使用してください

ソリューション:

=は/ usr / local / PHP / binに/ PHP-設定に./configure --with-PHP-コンフィグ

第三に、クイックスタート

a)はTCPサーバ

# 创建 php 文件
vi tcp_server.php
<?php
    //创建Server对象,监听 127.0.0.1:9501端口
    $serv = new Swoole\Server("127.0.0.1", 9501); 
    
    //监听连接进入事件
    $serv->on('Connect', function ($serv, $fd) {  
        echo "Client: Connect.\n";
    });
    
    //监听数据接收事件
    $serv->on('Receive', function ($serv, $fd, $from_id, $data) {
        $serv->send($fd, "Server: ".$data);
    });
    
    //监听连接关闭事件
    $serv->on('Close', function ($serv, $fd) {
        echo "Client: Close.\n";
    });
    
    //启动服务器
    $serv->start(); 
# 程序运行测试

# 运行 server
php tcp_server.php

# 使用telnet 测试(退出telnet:Ctrl+] 回车,输入quit 回车)
telnet 127.0.0.1 9501

b)はUDPサーバー

vi udp_server.php
<?php
    //创建Server对象,监听 127.0.0.1:9502端口,类型为SWOOLE_SOCK_UDP
    $serv = new swoole_server("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP); 
    
    //监听数据接收事件
    $serv->on('Packet', function ($serv, $data, $clientInfo) {
        $serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data);
        var_dump($clientInfo);
    });
    
    //启动服务器
    $serv->start(); 
# 运行
php udp_server.php

# 使用 netcat 连接
netcat -u 127.0.0.1 9502

C)HTTPサーバ

vi http_server.php
<?php
    $http = new Swoole\Http\Server("0.0.0.0", 9501);
    
    $http->on('request', function ($request, $response) {
        if ($request->server['path_info'] == '/favicon.ico' ||    $request->server['request_uri'] == '/favicon.ico') {
            return $response->end();
        }
        
        var_dump($request->get, $request->post);
        $response->header("Content-Type", "text/html; charset=utf-8");
        $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
    });
    
    $http->start();
# 程序运行测试

php http_server.php

# 浏览器访问
127.0.0.1:9501

D)のWebSocketサーバー

WebSocketのサーバーは、サーバーへの長いHTTP接続上に構築されたサーバーで、クライアントは、最初のサーバーのHTTPハンドシェイクにリクエストを送信します。開く時握手トリガイベントが成功した接続の準備ができ、開く時関数は、$ requestオブジェクトは、GETパラメータ、クッキー、HTTPヘッダー情報として、情報のHttpハンドシェイクが含まれ得ることができます。

接続クライアントとサーバの後双方向通信することができます。

vi ws_server.php
<?php
    //创建websocket服务器对象,监听0.0.0.0:9502端口
    $ws = new swoole_websocket_server("0.0.0.0", 9502);
    
    //监听WebSocket连接打开事件
    $ws->on('open', function ($ws, $request) {
        var_dump($request->fd, $request->get, $request->server);
        $ws->push($request->fd, "hello, welcome\n");
    });
    
    //监听WebSocket消息事件
    $ws->on('message', function ($ws, $frame) {
        echo "Message: {$frame->data}\n";
        $ws->push($frame->fd, "server: {$frame->data}");
    });
    
    //监听WebSocket连接关闭事件
    $ws->on('close', function ($ws, $fd) {
        echo "client-{$fd} is closed\n";
    });
    
    $ws->start();
# 程序运行

php ws_server.php
//使用浏览器JS代码如下

var wsServer = 'ws://127.0.0.1:9502';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {
    console.log("Connected to WebSocket server.");
};

websocket.onclose = function (evt) {
    console.log("Disconnected");
};

websocket.onmessage = function (evt) {
    console.log('Retrieved data from server: ' + evt.data);
};

websocket.onerror = function (evt, e) {
    console.log('Error occured: ' + evt.data);
};

E)タイマー

swooleはJavaScriptのsetInterval / setTimeoutを非同期精密タイマー、ミリ秒単位の粒子サイズが用意されています。

vi timer_tick.php
//每隔2000ms触发一次
$timerId = swoole_timer_tick(2000, function ($timer_id) {
    echo "tick-2000ms\n";
});

//9000ms后执行此函数
swoole_timer_after(9000, function () use ($timerId) {
    echo "after-9000ms.\n";
    
    //清除定时器
    swoole_timer_clear($timerId);
});
php timer_tick.php

f)は、同期、非同期TCPクライアント

vi tcp_client.php
<?php
    $client = new swoole_client(SWOOLE_SOCK_TCP);
    
    //连接到服务器
    if (!$client->connect('127.0.0.1', 9501, 0.5))
    {
        die("connect failed.");
    }
    //向服务器发送数据
    if (!$client->send("hello world"))
    {
        die("send failed.");
    }
    //从服务器接收数据
    $data = $client->recv();
    if (!$data)
    {
        die("recv failed.");
    }
    echo $data;
    //关闭连接
    $client->close();

# 异步只能用于cli
vi tcp_async_client.php
<?php
    $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
    
    //注册连接成功回调
    $client->on("connect", function($cli) {
        $cli->send("hello world\n");
    });
    
    //注册数据接收回调
    $client->on("receive", function($cli, $data){
        echo "Received: ".$data."\n";
    });
    
    //注册连接失败回调
    $client->on("error", function($cli){
        echo "Connect failed\n";
    });
    
    //注册连接关闭回调
    $client->on("close", function($cli){
        echo "Connection close\n";
    });
    
    //发起连接
    $client->connect('127.0.0.1', 9501, 0.5);

グラム)クライアントコルーチン

vi xxx.php
<?php
    $http = new swoole_http_server("0.0.0.0", 9501);
    
    $http->on('request', function ($request, $response) {
        $db = new Swoole\Coroutine\MySQL();
        $db->connect([
            'host' => '127.0.0.1',
            'port' => 3306,
            'user' => 'user',
            'password' => 'pass',
            'database' => 'test',
        ]);
        $data = $db->query('select * from test_table');
        $response->end(json_encode($data));
    });
    
    $http->start();

H)コルーチン:同時もしくはshell_exec

vi xxx.php
<?php
    $c = 10;
    while($c--) {
        go(function () {
            //这里使用 sleep 5 来模拟一个很长的命令
            co::exec("sleep 5");
        });
    }

ⅰ)协程:GO +チャン+延期

公式ドキュメントを参照してください。
https://wiki.swoole.com/wiki/page/p-csp.html

おすすめ

転載: www.cnblogs.com/reader/p/11665154.html
おすすめ