tp5.1使用think-swoole
1.下载tp框架,版本5.1.35 ,下载think-swoole,版本2.0.17
composer require topthink/think-swoole=2.0.*
2.在config下书写swoole.php
<?php use think\facade\Env; // +---------------------------------------------------------------------- // | Swoole设置 php think swoole命令行下有效 // +---------------------------------------------------------------------- return [ // 扩展自身配置 'host' => '0.0.0.0', // 监听地址 'port' => 9501, // 监听端口 'worker_num' => 4, 'max_request' => 1000, 'app_path' => '/web/twzb/application/', 'cache_size'=>1024*8, /* 'mode' => '', // 运行模式 默认为SWOOLE_PROCESS 'sock_type' => '', // sock type 默认为SWOOLE_SOCK_TCP 'server_type' => 'http', // 服务类型 支持 http websocket 'app_path' => '', // 应用地址 如果开启了 'daemonize'=>true 必须设置(使用绝对路径) 'file_monitor' => false, // 是否开启PHP文件更改监控(调试模式下自动开启) 'file_monitor_interval' => 2, // 文件变化监控检测时间间隔(秒) 'file_monitor_path' => [], // 文件监控目录 默认监控application和config目录 // 可以支持swoole的所有配置参数 'pid_file' => Env::get('runtime_path') . 'swoole.pid', 'log_file' => Env::get('runtime_path') . 'swoole.log', 'document_root' => Env::get('root_path') . 'public', 'enable_static_handler' => true, 'timer' => true,//是否开启系统定时器 'interval' => 500,//系统定时器 时间间隔 'task_worker_num' => 1,//swoole 任务工作进程数量*/ ];
2.在config下书写swoole_server.php
<?php use think\facade\Env; // +---------------------------------------------------------------------- // | Swoole设置 php think swoole:server 命令行下有效 // +---------------------------------------------------------------------- return [ 'swoole_class' => 'app\http\Swoole', // 自定义服务类名称 ];
3.书写Swoole.php
<?php
namespace app\http;
use think\swoole\Server;
class Swoole extends Server
{
protected $host = '0.0.0.0';
protected $port = 9502;
protected $serverType = 'socket';
protected $mode = SWOOLE_PROCESS;
protected $sockType = SWOOLE_SOCK_TCP;
protected $option = [
'worker_num'=> 4,
'daemonize' => true,
'backlog' => 128
];
public function onReceive($server, $fd, $from_id, $data)
{
$server->send($fd, 'Swoole: '.$data);
}
public function onRequest($request, $response)
{
// $server->send('Swoole: ');
}
/**
* 监听ws消息事件
* @param $ws
* @param $frame
*/
public function onMessage($ws, $frame) {
echo "ser-push-message:{$frame->data}\n";
$ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
}
}
使用命令
php think swoole
使用命令
php think swoole:server
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)