Thinkcmf5.0集成Swoole

博客日记 zhangshun 8个月前 (12-30) 652次浏览 已收录 0个评论
<?php
class ws{
private $_host;
private $_port;
protected $ws;

public function __construct($host,$port){
$this->_host = $host;
$this->_port = $port;
$this->ws = new swoole_websocket_server($this->_host,$this->_port);
$this->ws->set([
'enable_static_handler' => true,
'worker_num' => 2,
'task_worker_num' => 2,
]);

$this->ws->on('open',[$this,'open']);
$this->ws->on('message',[$this,'message']);
$this->ws->on('WorkerStart',[$this,'workStart']);
$this->ws->on('request',[$this,'request']);
$this->ws->on('task',[$this,'task']);
$this->ws->on('finish',[$this,'finish']);
$this->ws->on('close',[$this,'close']);
$this->ws->start();
}

public function workStart($serv,$worker_id){
// 调试模式开关
define("APP_DEBUG", false);
define("APP_NAMESPACE", 'api');
define('CMF_ROOT', __DIR__ . '/../../');
define('APP_PATH', CMF_ROOT . 'api/');
define('CMF_PATH', __DIR__ . '/../../simplewind/cmf/');
define('PLUGINS_PATH', __DIR__ . '/../plugins/');
define('EXTEND_PATH', __DIR__ . '/../../simplewind/extend/');
define('VENDOR_PATH', __DIR__ . '/../../simplewind/vendor/');
define('RUNTIME_PATH',__DIR__.'/../../data/runtime/api/');

require CMF_ROOT . 'simplewind/thinkphp/base.php'; //只是加载框架的核心文件

//如果要在其他回调里面用 tp 里面的东西就要在这初始化一下,例如 task 里面的,如果不这样搞,直接报找不到这个类的错误
\think\App::initCommon();

}
public function request($request,$response){
// //初始化超全局数据
$_SERVER = [];
if(isset($request->server)){
foreach($request->server as $k => $v){
$_SERVER[strtoupper($k)] = $v;
}
}
if(isset($request->header)){
foreach($request->header as $k => $v){
$_SERVER[strtoupper($k)] = $v;
}
}
$_GET = [];
if(isset($request->get)){
foreach($request->get as $k => $v){
$_GET[$k] = $v;
}
}
$_FILES = [];
if(isset($request->files)){
foreach($request->files as $k => $v){
$_FILES[$k] = $v;
}
}
$_POST = [];
if(isset($request->post)){
foreach($request->post as $k => $v){
$_POST[$k] = $v;
}
}
$_POST['http_server'] = $this->ws;
$_COOKIE = [];
if(isset($request->cookie)){
foreach($request->cookie as $k => $v){
$_COOKIE[$k] = $v;
}
}
ob_start();
// 执行应用并响应
\think\App::run()->send();
$res = ob_get_contents();
ob_end_clean();
$response->end($res);
}
/**
* @param $server 就是初始化的 server 服务本身
* @param $task_id
* @param $worker_id
* @param $data
* @return mixed
*/
public function task($server,$task_id,$worker_id,$data){
$task = new \api\home\service\TaskService();
$result = $task->task($data['method'],$data['data'],$server);
if(is_array($result)){
$result = json_encode($result);
}
return $result;
}
/**
* finish 回调函数中的 data 是 task 回调的返回值
* @param $server
* @param $task_id
* @param $data
*/
public function finish($server,$task_id,$data){
echo "taskId:{$task_id}\n";
echo "finish-data-success:{$data}";
}
public function open($server,$request){
//print_r($server);
echo "server: connect success!{$request->fd}";
}
public function message($server,$frame){
//获取唯一标示符
$fd = $frame->fd;
$username = $frame->data;
}
public function close($server,$fd){
echo "{$fd} 已经断开连接!";
}
}
$http = new ws('0.0.0.0',9911);

章顺的个人博客, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Thinkcmf5.0 集成 Swoole
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址