Download the PHP package lifetime/swoole-server without Composer
On this page you can find all versions of the php package lifetime/swoole-server. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lifetime/swoole-server
More information about lifetime/swoole-server
Files in lifetime/swoole-server
Package swoole-server
Short Description a simple encapsulation of Swoole services
License
Informations about the package swoole-server
Swoole 服务器
swoole-server 对swoole的各类服务器进行一个简单的封装,让使用的时候更加方便。
已对以下服务器进行封装
- TCP/UDP
- HTTP
- WebSocket
- MQTT
配置类 \swoole\Config
\swoole\Config
包含swoole服务器的所有配置项,详情参考 配置
\swoole\Config
类有三种使用方法
第一种:
第二种:
第三种:
参数需要参考swoole官方文档,前两种方法采用下划线命名,第三种类属性需要采用小驼峰命名
常量参数类 \swoole\Options
\swoole\Options
包含了一些常用的swoole常量,比如:运行模式、Server类型、Task 进程与 Worker 进程之间通信的方式等。
事件类
所有的事件类都在 \swoole\event
命名空间下,每个服务器有相对应的事件类,开发时,可以继承事件类,重写对应的事件;然后在 Config
中设置 event_class
为事件类的名称,注意,必须包含完整的命名空间。
MQTT 的事件类中 为了不与 swoole 的 onConnect
事件冲突,MQTT业务的连接事件为 onMqttConnect
。
服务器类
所有的服务器类都在 \swoole\server
命名空间下
通过几行代码就可以启动一个服务器:
如果你想在启动之前做一些其他事情,可以这样操作
服务类的
getServer
方法会返回 swoole 的 Server 实例,可以进行其他的操作。
如果你不喜欢启动、停止时默认输出的消息的话,你也可以自定义消息。如:
getMasterPid
可以获取服务器管理进程的pid,注意:必须是和启动时相同的配置。
isRuning
可以获取指定服务器的运行状态。
文件监控
文件监控需要配置 Config
的 file_moniotr
和 file_list
选项;文件必须是绝对路径。
有几点要注意:
首先要注意新修改的代码必须要在
OnWorkerStart
事件中重新载入才会生效,比如某个类在OnWorkerStart
之前就通过 composer 的 autoload 载入了就是不可以的。其次 reload 还要配合这两个参数
max_wait_time
和reload_async
,设置了这两个参数之后就能实现异步安全重启。
定时器服务
配置
任务类
任务类继承 \swoole\extend\abstcats
抽象类,需要完成 hanndle
方法
crontab
属性参照 Linux 的 Crontab 配置
注意:这里比 Linux 的 Crontab 多了一个秒
启动
除了 start
之外,还有 stop
、reload
和restart
方法。
All versions of swoole-server with dependencies
ext-swoole Version >=4.5.8