Swoole介绍
Swoole: PHP的异步、并行、高性能网络通信引擎,支持TCP长连接,Websocket,Mqtt等协议。广泛用于手机app、手游服务端、网络游戏服务器、聊天室、硬件通讯、智能家居、车联网、物联网等领域的开发。
注意
目前改项目尚未成熟,可能会有未知问题,如果你的项目比较复杂,不建议使用。
安装Swoole
如果你使用的是宝塔面板,那么在PHP配置面板中安装拓展即可。
其他面板或自建环境,请参照官方安装文档。https://wiki.swoole.com/#/environment
配置Nginx规则
如果使用了nginx,可以配置一下规则进行转发。配置此规则前,请将伪静态的规则删除。
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server
{
#新增 --- start ---
location = /index.php {
# Ensure that there is no such file named "not_exists"
# in your "public" directory.
try_files /not_exists @swoole;
}
# any php files must not be accessed
#location ~* \.php$ {
# return 404;
#}
location / {
try_files $uri $uri/ @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# IF https
# proxy_set_header HTTPS "on";
proxy_pass http://127.0.0.1:1215$suffix;
}
#新增 --- end ---
}
启动Swoole加速
如果使用的是最新版的源码,那么可以直接启动Swoole了。
在站点根目录下执行命令 php swoole.php start 即可启动
php php swoole.php start|stop|restart|reload
获取客户端IP
使用 get_client_ip 函数获取
热更新
后台清除缓存操作进行热更新