用swoole实现websocket消息推送怎么做
Admin 2022-06-08 群英技术资讯 581 次浏览
需求
需要实现一个可以主动触发消息推送的功能,这个可以实现向模板消息那个,给予所有成员发送自定义消息,而不需要通过客户端发送消息,服务端上message中监听传送的消息进行做相对于的业务逻辑。
主动消息推送实现
平常我们采用 swoole 来写 WebSocket 服务可能最多的用到的是open,message,close这三个监听状态,但是万万没有看下下面的onRequest回调的使用,没错,解决这次主动消息推送的就是需要用onRequest回调。
官方文档:正因为swoole_websocket_server继承自swoole_http_server,所以在 websocket 中有onRequest回调。
详细实现:
# 这里是一个laravel中Commands # 运行php artisan swoole start 即可运行 <?php namespace App\Console\Commands; use Illuminate\Console\Command; use swoole_websocket_server; class Swoole extends Command { public $ws; /** * The name and signature of the console command. * * @var string */ protected $signature = 'swoole {action}'; /** * The console command description. * * @var string */ protected $description = 'Active Push Message'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $arg = $this->argument('action'); switch ($arg) { case 'start': $this->info('swoole server started'); $this->start(); break; case 'stop': $this->info('swoole server stoped'); break; case 'restart': $this->info('swoole server restarted'); break; } } /** * 启动Swoole */ private function start() { $this->ws = new swoole_websocket_server("0.0.0.0", 9502); //监听WebSocket连接打开事件 $this->ws->on('open', function ($ws, $request) { }); //监听WebSocket消息事件 $this->ws->on('message', function ($ws, $frame) { $this->info("client is SendMessage\n"); }); //监听WebSocket主动推送消息事件 $this->ws->on('request', function ($request, $response) { $scene = $request->post['scene']; // 获取值 $this->info("client is PushMessage\n".$scene); }); //监听WebSocket连接关闭事件 $this->ws->on('close', function ($ws, $fd) { $this->info("client is close\n"); }); $this->ws->start(); } }
前面说的是 swoole 中onRequest的实现,下面实现下在控制器中主动触发onRequest回调。实现方法就是我们熟悉的curl请求。
# 调用activepush方法以后,会在cmd中打印出 # client is PushMessage 主动推送消息 字眼 /** * CURL请求 * @param $data */ public function curl($data) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://127.0.0.1:9502"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_exec($curl); curl_close($curl); } /** * 主动触发 */ public function activepush() { $param['scene'] = '主动推送消息'; $this->curl($param); // 主动推送消息
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文主要带大家来看看,给定一个三位整数,怎么判断该数是不是水仙花数?然后怎么输出全部水仙花数。首先我们来看看什么是水仙花数?水仙花数是一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)
PHP中常见的header头定义有哪些?对PHP新手来说,对这个问题可能不是很清楚,这篇文章就给大家介绍一下,了解一些常用header头定义还是很有用处的,下面一起来看一下。
这篇文章主要介绍了Laravel5.1 框架文件管理操作,结合实例形式分析了laravel5.1框架文件管理相关的配置、磁盘获取以及文件目录操作技巧,需要的朋友可以参考下
今天小编就为大家分享一篇php 比较获取两个数组相同和不同元素的例子(交集和差集),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
界面使用Material Theme UI插件,然后Install,再Restart,Done!Laravel Plugin当你把一个Laravel项目导入到PhpStorm时,你需要下一个Laravel Plugin插件。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008