初識 swoole [上]

OMGZui發表於2018-11-06

前言:都是為了生存

有收穫的話請加顆小星星,沒有收穫的話可以 反對 沒有幫助 舉報三連

一、什麼是swoole

Swoole:面向生產環境的 PHP 非同步網路通訊引擎

使 PHP 開發人員可以編寫高效能的非同步併發 TCP、UDP、Unix Socket、HTTP,WebSocket 服務。Swoole 可以廣泛應用於網際網路、行動通訊、企業軟體、雲端計算、網路遊戲、物聯網(IOT)、車聯網、智慧家居等領域。 使用 PHP + Swoole 作為網路通訊框架,可以使企業 IT 研發團隊的效率大大提升,更加專注於開發創新產品。

二、安裝

1、pecl

pecl install swoole

2、原始碼安裝

curl -O https://pecl.php.net/get/swoole-4.2.5.tgz
tar -zxvf swoole-4.2.5.tgz
cd swoole-4.2.5.tgz
phpize
./configure
make && make install
# 加入到php.ini中
php --ini
extension=swoole.so

三、基本入門

注意:示例程式碼都引入了"symfony/var-dumper"包進行美化列印

Swoole的絕大部分功能只能用於cli命令列環境

1、TCP伺服器

<?php
// new
$server = new swoole_server("0.0.0.0", "9501",SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

// connect 連線
$server->on("connect", function (swoole_server $server, $fd) {
    dump("{$fd}連線");
    $server->send($fd, "歡迎{$fd}大山驢\n");
});
// receive 回撥
$server->on("receive", function (swoole_server $server, $fd, $from_id, $data) {
    $server->send($fd, "服務端回覆:{$data}\n");
    foreach ($server->connections as $connection) {
        if ($connection != $fd){
            $server->send($connection, "{$fd}說{$data}");
        }
    }

});
// close
$server->on("close", function (swoole_server $server, $fd) {
    dump("{$fd}關閉");
    foreach ($server->connections as $connection) {
        if ($connection != $fd){
            $server->send($connection, "{$fd}斷開連線");
        }
    }
});
// start
$server->start();
php tcp_server.php

telnet 127.0.0.1 9501

2、UDP伺服器

// new
<?php
$server = new swoole_server("0.0.0.0", "9502", SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
// packet
$server->on("Packet", function (swoole_server $server, $data, $clientInfo) {
    $server->sendto($clientInfo['address'], $clientInfo['port'], "伺服器回覆: {$data}");
    dump($clientInfo);
});
// start
$server->start();
php udp_server.php

netcat -u 127.0.0.1 9502

3、Web伺服器

<?php
// new
$http = new swoole_http_server(HOST, "9503");
// request
$http->on("request", function (\Swoole\Http\Request $request, \Swoole\Http\Response $response) {
    dump($request->get, $request->post);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #" . mt_rand(1000, 9999) . "</h1>\n");
});
// start
$http->start();
php web_server.php

curl -XGET "127.0.0.1:9503?id=1&name=aa&age=26"
curl -XPOST "127.0.0.1:9503?id=1&name=aa&age=26" -d "love=like"

4、WebSocket伺服器

服務端

<?php
// new
$ws = new swoole_websocket_server(HOST, "9504");
// open
$ws->on("open", function (Swoole\WebSocket\Server $ws, \Swoole\Http\Request $request) {
    dump($request->fd, $request->get, $request->server);
    $ws->push($request->fd, "你是大山驢\n");
});
// message
$ws->on("message", function (\Swoole\WebSocket\Server $ws, $frame) {
    dump("訊息: {$frame->data}\n");
    $ws->push($frame->fd, "服務端回覆: {$frame->data}\n");
});
// close
$ws->on("close", function (Swoole\WebSocket\Server $ws, $fd) {
    dump("{$fd}關閉");
});
// start
$ws->start();

客戶端

let ws = new WebSocket("ws://127.0.0.1:9504");
ws.onopen = function (ws) {
    console.log("連線伺服器");
};

ws.onclose = function (ws) {
    console.log("斷開連線");
};

ws.onmessage = function (ws) {
    console.log('接收來自伺服器的訊息:' + ws.data);
};

ws.onerror = function (ws, event) {
    console.log('錯誤了:' + ws.data);
};

5、定時器

<?php
// 每2秒執行一次
swoole_timer_tick(2000, function ($time_id){
    dump($time_id);
});

// 3秒後執行
swoole_timer_after(3000, function (){
    dump("這是3s後");
});
本作品採用《CC 協議》,轉載必須註明作者和本文連結