目录

Swoole 起步

Swoole 虽然是标准的 PHP 扩展,实际上与普通的扩展不同。普通的扩展只是提供一个库函数。而 Swoole 扩展在运行后会接管 PHP 的控制权,进入事件循环。当 IO 事件发生后底层会自动回调指定的 PHP 函数。说它重新定义了 PHP,一点也不夸张。

概述

Swoole 是面向生产环境的 PHP 协程框架。使 PHP 开发人员可以编写高性能的协程 TCP、UDP、Unix Socket、HTTP,WebSocket 服务。Swoole 可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。 使用 PHP + Swoole 作为网络通信框架,可以使企业 IT 研发团队的效率大大提升,更加专注于开发创新产品。

安装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apt-get update && apt-get install vim -y && \
apt-get install openssl -y && \
apt-get install libssl-dev -y && \
apt-get install wget -y && \
apt-get install git -y && \
apt-get install procps -y && \
apt-get install htop -y

cd /usr/local/src && \
git clone https://github.com/swoole/swoole-src.git && \
cd swoole-src && \
git checkout v4.4.8 && \
phpize && \
./configure --enable-openssl && \
make && make install

touch /usr/local/etc/php/conf.d/swoole.ini && echo 'extension=swoole.so' > /usr/local/etc/php/conf.d/swoole.ini

使用

创建 HTTP 服务器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php

$http = new \Swoole\Http\Server('127.0.0.1', 8080);
$http->on('start', static function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:8080\n";
});
$http->on('request', static function ($request, $response) {
    $response->header('Content-Type', 'text/plain');
    $response->end("Hello World!\n");
});
$http->start();

创建 WebSocket 服务器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php

$websocket = new \Swoole\WebSocket\Server('127.0.0.1', 8080);
$websocket->on('open', static function ($server, $request) {
    echo "Connection open: {$request->fd}\n";
});
$websocket->on('message', static function ($server, $frame) {
    echo "Received message: {$frame->data}\n";
    $server->push($frame->fd, json_encode(['hello', 'world']));
});
$websocket->on('close', static function ($server, $fd) {
    echo "Connection close: {$fd}\n";
});
$websocket->start();

创建 TCP 服务器

TCP 服务器:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php

$server = new \Swoole\Server('127.0.0.1', 8080);
$server->on('connect', static function ($server, $request) {
    echo "Connection open: {$request->fd}\n";
});
$server->on('receive', static function ($server, $fd, $id, $data) {
    $server->send($fd, "Swoole: {$data}");
    $server->close($fd);
});
$server->on('close', static function ($server, $fd) {
    echo "Connection close: {$fd}\n";
});
$server->start();

TCP 客户端:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

$client = new \Swoole\Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on('connect', static function ($client) {
    $client->send("Hello World!\n");
});
$client->on('receive', static function ($client, $data) {
    echo "Received: {$data}\n";
});
$client->on('error', static function ($client) {
    echo "Connection failed\n";
});
$client->on('close', static function ($client) {
    echo "Connection close\n";
});
$client->connect('127.0.0.1', 8080, 0.5);

问题

  • error "Enable openssl support, require openssl library.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 确定 openssl 位置
which openssl
/usr/local/opt/openresty-openssl/bin/openssl

// 安装 swoole 时指定 openssl 路径
pecl install swoole
downloading swoole-4.4.8.tgz ...
Starting to download swoole-4.4.8.tgz (1,427,235 bytes)
.........................................................................................................................................................................................................................................................................................done: 1,427,235 bytes
391 source files, building
running: phpize
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
enable sockets supports? [no] : yes
enable openssl support? [no] : yes --with-openssl-dir=/usr/local/opt/openssl@1.1/bin/openssl
enable http2 support? [no] : yes
enable mysqlnd support? [no] : yes
building in /private/tmp/pear/temp/pear-build-majinyunwRSXz0/swoole-4.4.8

// 安装成功信息
Build process completed successfully
Installing '/usr/local/Cellar/php/7.3.10/include/php/ext/swoole/config.h'
Installing '/usr/local/Cellar/php/7.3.10/pecl/20180731/swoole.so'
install ok: channel://pecl.php.net/swoole-4.4.8
Extension swoole enabled in php.ini