作者:jessiemiumiu_956 | 来源:互联网 | 2023-07-01 11:44
Laravel配置Swoole作为websocket服务器因为公司要做一个学生在线答题—老师监控的功能,并且可以实时同步学生的答题状态所以配置了swooleÿ
Laravel配置Swoole作为websocket服务器
因为公司要做一个学生在线答题 — 老师监控的功能,并且可以实时同步学生的答题状态 所以配置了swoole,当做websocket服务器。我这里用的扩展是laravel-s 来实现一个类似聊天室的功能。
版本信息
- Laravel 8.0
- hhxsv5/laravel-s 3.7
- Swoole扩展 4.5.6
- PHP 7.3
安装并配置Laravel-s扩展
-
composer require "hhxsv5/laravel-s:~3.7.0" -vvv
-
发布laravel-s的配置文件 php artisan laravels publish
-
生成的文件在config/laravels.php
中,需要关注的就几项:
'listen_ip' => env('LARAVELS_LISTEN_IP', '127.0.0.1'),
'listen_port' => env('LARAVELS_LISTEN_PORT', 5200),
'websocket' => [ 'enable' => true, 'handler' => \App\Servers\WebSocketService::class,
],
'swoole' => ['dispatch_mode' => env('LARAVELS_DISPATCH_MODE', 2),
],
这里laravels需要一个handler
类来响应websocket的各种事件,具体的代码 我会放到文末,也可以直接在laravels
文档中获取
配置nginx
这是laravels 默认的nginx配置文件 修改一下绑定的host就行了
gzip on;
gzip_min_length 1024;
gzip_comp_level 2;
gzip_types text/plain text/css text/Javascript application/json application/Javascript application/x-Javascript application/xml application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
gzip_vary on;
gzip_disable "msie6";
upstream swoole {# 通过 IP:Port 连接 这里的ip和端口 应该config中的配置保持一致server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s;# 通过 UnixSocket Stream 连接,小诀窍:将socket文件放在/dev/shm目录下,可获得更好的性能#server unix:/yourpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s;#server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s;#server 192.168.1.2:5200 backup;keepalive 16;
}
server {listen 80;# 别忘了绑Hostserver_name laravels.com;root /yourpath/laravel-s-test/public;access_log /yourpath/log/nginx/$server_name.access.log main;autoindex off;index index.html index.htm;# Nginx处理静态资源(建议开启gzip),LaravelS处理动态资源。location / {try_files $uri @laravels;}# 当请求PHP文件时直接响应404,防止暴露public/*.php#location ~* \.php$ {# return 404;#}location @laravels {# proxy_connect_timeout 60s;# proxy_send_timeout 60s;# proxy_read_timeout 120s;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Real-PORT $remote_port;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Host $http_host;proxy_set_header Scheme $scheme;proxy_set_header Server-Protocol $server_protocol;proxy_set_header Server-Name $server_name;proxy_set_header Server-Addr $server_addr;proxy_set_header Server-Port $server_port;# “swoole”是指upstreamproxy_pass http://swoole;}
}
通过supervisord守护进程
- 添加
supervisord
配置文件 配置文件目录一般在 /etc/supervisor/conf.d
[program:laravel-s-test]
directory=/www/laravel
command=/usr/bin/php bin/laravels start -i
numprocs=1
autostart=true
autorestart=true
startretries=3
user=www-data
redirect_stderr=true
stdout_logfile=/var/log/supervisor/%(program_name)s.log
directory
设置为laravel项目根目录即可。
command
中需要正确定义 php
可执行命令的地址。
stdout_logfile
是输出的日志文件,需要保证user
配置项中的用户有可写权限。
- 载入配置并重启
supervisord
supervisorctl update && supervisorctl reload
尾巴
这篇主要记录一下配置之类的,免得自己忘记,其实可以直接看laravels
的配置文档。
下一篇会记录 WebSocketService
类中对websock
事件的响应的业务代码
- Laravels 中文文档:https://github.com/hhxsv5/laravel-s/blob/master/README-CN.md
- Laravel 文档:https://learnku.com/docs/laravel/8.x