thinkphp5swoole执行异步任务

thinkphp5swoole执⾏异步任务⽬录结构:
电视剧谍影重重之上海
服务器端:
<?php
/*
*author:hdj
*/
namespace app\Console;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class Websocket extends Command{
protected$server;
protected function configure()
瓜棚女杰{
$this->setName('websocket:start')->setDescription('Start Web Socket Server!');
}
protected function execute(Input $input, Output $output)
骈体文钞{
gfp
$serv = new \swoole_server('0.0.0.0',9501);
$serv->set(array('task_worker_num' => 4));
$serv->on('connect', function ($serv, $fd){
echo$fd."客户端已经连接进来了.\n";
});
$serv->on('receive', function($serv, $fd, $from_id, $data) {
$task_id = $serv->task($data);
echo "开始投递异步任务 id=$task_id\n";
});
$serv->on('task', function ($serv, $task_id, $from_id, $data) {
echo "接收异步任务[id=$task_id]".PHP_EOL;
for ($i = 0 ; $i<10000;$i++){
if($i%2==0){
fmcm
echo 'send'.$i.' success'."\n";
}else{
echo 'send'.$i.' fail'."\n";
}
sleep(1);
}
$serv->finish("$data -> OK");
});
$serv->on('finish', function ($serv, $task_id, $data) {
echo "异步任务[id=$task_id]完成".PHP_EOL;
});
$serv->start();
}
}
进⼊你的根⽬录执⾏  php think websocket:start
客户端:
<?php
namespace app\index\controller;
use think\Controller;
class Test extends Controller
{
public function index(){
$client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
$ret = $client->connect("23.27.127.32", 9501);
if(empty($ret)){
echo 'error!connect to swoole_server failed';
} else {
$client->send('blue');
百慕高科
}
}
}
服务端显⽰:

本文发布于:2024-09-20 18:27:49,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/153117.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:任务   谍影   连接   瓜棚
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议