Centos7在安装完php7.3的基础上安装swoole
源码下载和安装
yum install autoconf
wget https://github.com/swoole/swoole-src/archive/v4.4.12.tar.gz
tar zxvf v4.4.12.tar.gz
cd swoole-src-4.4.12/
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
sudo make install
sudo vim /usr/local/php/lib/php.ini //添加 extension=swoole
php -m //查看扩展是否安装成功
测试
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();
运行源码
php server.php
此时服务端处于阻塞状态
telnet模拟客户端连接
yum -y install telnet
连接服务端
telnet 127.0.0.1 9501
此时服务端会提示Client: Connect.
客户端会提示:
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
此时在客户端打印hello,服务端会返回hello
# telnet 127.0.0.1 9501
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello
Server: hello
断开连接
ctrl+] 用于退出当前连接。此时服务端会打印Client: Close.
再输入q退出telnet
————————————————
原文链接:https://blog.csdn.net/Edu_enth/article/details/103180524