Laravel使用swoole实现websocket主动消息推送的方法介绍


Posted in PHP onOctober 20, 2019

需求

需要实现一个可以主动触发消息推送的功能,这个可以实现向模板消息那个,给予所有成员发送自定义消息,而不需要通过客户端发送消息,服务端上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);   // 主动推送消息

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。

PHP 相关文章推荐
一个可以找出源代码中所有中文的工具
Oct 25 PHP
fleaphp rolesNameField bug解决方法
Apr 23 PHP
PHP生成图片验证码、点击切换实例
Jun 25 PHP
PHP编程中的常见漏洞和代码实例
Aug 06 PHP
分享一段PHP制作的中文拼音首字母工具类
Dec 11 PHP
10个超级有用值得收藏的PHP代码片段
Jan 22 PHP
php自定义hash函数实例
May 05 PHP
WordPress中用于更新伪静态规则的PHP代码实例讲解
Dec 18 PHP
php使用正则表达式去掉html中的注释方法
Nov 03 PHP
php页面跳转session cookie丢失导致不能登录等问题的解决方法
Dec 12 PHP
php实现微信支付之现金红包
May 30 PHP
php apache开启跨域模式过程详解
Jul 08 PHP
laravel 解决路由除了根目录其他都404的问题
Oct 18 #PHP
Laravel基础-关于引入公共文件的两种方式
Oct 18 #PHP
关于laravel模板中生成URL的几种模式总结
Oct 18 #PHP
Laravel 前端资源配置教程
Oct 18 #PHP
tp5 实现列表数据根据状态排序
Oct 18 #PHP
tp5递归 无限级分类详解
Oct 18 #PHP
确保Laravel网站不会被嵌入到其他站点中的方法
Oct 18 #PHP
You might like
Fatal error: session_start(): Failed to initialize storage module: files问题解决方法
2014/05/04 PHP
qq登录,新浪微博登录接口申请过程中遇到的问题
2014/07/22 PHP
php实现的IMEI限制的短信验证码发送类
2015/05/05 PHP
PHP使用imagick扩展实现合并图像的方法
2017/04/25 PHP
JS 无限级 Select效果实现代码(json格式)
2011/08/30 Javascript
让图片旋转任意角度及JQuery插件使用介绍
2013/03/20 Javascript
JS防止用户多次提交的简单代码
2013/08/01 Javascript
jquery跟js初始化加载的多种方法及区别介绍
2014/04/02 Javascript
关于JavaScript命名空间的一些心得
2014/06/07 Javascript
jquery实现增加删除行的方法
2015/02/03 Javascript
javascript实现汉字转拼音代码分享
2015/04/20 Javascript
通过实例理解javascript中没有函数重载的概念
2015/06/03 Javascript
获取JS中网页各种高宽与位置的方法总结
2016/07/27 Javascript
轻松掌握JavaScript单例模式
2016/08/25 Javascript
基于JQuery及AJAX实现名人名言随机生成器
2017/02/10 Javascript
jQuery插件FusionCharts实现的3D帕累托图效果示例【附demo源码】
2017/03/25 jQuery
[03:05]DOTA2英雄基础教程 嗜血狂魔
2013/12/10 DOTA
Django的HttpRequest和HttpResponse对象详解
2018/01/26 Python
python opencv 图像尺寸变换方法
2018/04/02 Python
dataframe 按条件替换某一列中的值方法
2019/01/29 Python
python3用PIL把图片转换为RGB图片的实例
2019/07/04 Python
python matplotlib饼状图参数及用法解析
2019/11/04 Python
如何基于python对接钉钉并获取access_token
2020/04/21 Python
用CSS3绘制三角形的简单方法
2015/07/17 HTML / CSS
HTML5中语义化 b 和 i 标签
2008/10/17 HTML / CSS
HTML5地理定位实例
2014/10/15 HTML / CSS
暇步士官网:Hush Puppies
2016/09/22 全球购物
Doyoueven官网:澳大利亚健身服饰和配饰品牌
2019/03/24 全球购物
AJAX应用和传统Web应用有什么不同
2013/08/24 面试题
工商管理实习生自我鉴定范文
2013/12/18 职场文书
房产销售经理职责
2013/12/20 职场文书
怎样填写就业意向
2014/04/02 职场文书
自我推荐信范文
2014/05/09 职场文书
2015新学期开学寄语
2015/02/26 职场文书
建议书的格式及范文
2015/09/14 职场文书
golang判断key是否在map中的代码
2021/04/24 Golang