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 相关文章推荐
请php正则走开
Mar 15 PHP
fleaphp crud操作之findByField函数的使用方法
Apr 23 PHP
PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
Nov 10 PHP
php中$_GET与$_POST过滤sql注入的方法
Nov 03 PHP
php实现的RSS生成类实例
Apr 23 PHP
PHP Echo字符串的连接格式
Mar 07 PHP
YII Framework框架教程之国际化实现方法
Mar 14 PHP
PHP Ajax实现无刷新附件上传
Aug 17 PHP
PHP实现腾讯与百度坐标转换
Aug 05 PHP
PHP的cookie与session原理及用法详解
Sep 27 PHP
yii 框架实现按天,月,年,自定义时间段统计数据的方法分析
Apr 04 PHP
laravel使用redis队列实例讲解
Mar 23 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
PHP 开发环境配置(Zend Server安装)
2010/04/28 PHP
php二维数组排序与默认自然排序的方法介绍
2013/04/27 PHP
PHP类继承 extends使用介绍
2014/01/14 PHP
php中文繁体和简体相互转换的方法
2015/03/21 PHP
php结合正则批量抓取网页中邮箱地址
2015/05/19 PHP
thinkPHP简单导入和使用阿里云OSSsdk的方法
2017/03/15 PHP
js获取内联样式的方法
2015/01/27 Javascript
使用pjax实现无刷新更改页面url
2015/02/05 Javascript
js淡入淡出焦点图幻灯片效果代码分享
2015/09/08 Javascript
原生js实现数字字母混合验证码的简单实例
2015/12/10 Javascript
JS组件系列之Bootstrap table表格组件神器【终结篇】
2016/05/10 Javascript
AngularJs html compiler详解及示例代码
2016/09/01 Javascript
AngularJs Understanding the Model Component
2016/09/02 Javascript
JS利用cookies设置每隔24小时弹出框
2017/04/20 Javascript
深入理解JavaScript创建对象的多种方式以及优缺点
2017/06/01 Javascript
jQuery插件artDialog.js使用与关闭方法示例
2017/10/09 jQuery
js中Generator函数的深入讲解
2019/04/07 Javascript
javascript定时器的简单应用示例【控制方块移动】
2019/06/17 Javascript
利用vue-i18n实现多语言切换效果的方法
2019/06/19 Javascript
TypeScript 引用资源文件后提示找不到的异常处理技巧
2020/07/15 Javascript
详解python中字典的循环遍历的两种方式
2017/02/07 Python
Python File readlines() 使用方法
2018/03/19 Python
Python 实现异步调用函数的示例讲解
2018/10/14 Python
利用Python求阴影部分的面积实例代码
2018/12/05 Python
python实现网站用户名密码自动登录功能
2019/08/09 Python
使用Python调取任意数字资产钱包余额功能
2019/08/15 Python
Python TCP通信客户端服务端代码实例
2019/11/21 Python
用python解压分析jar包实例
2020/01/16 Python
如何理解Python中的变量
2020/06/01 Python
python统计mysql数据量变化并调用接口告警的示例代码
2020/09/21 Python
可持续未来的时尚基础:Alternative Apparel
2019/05/06 全球购物
2015个人年度工作总结范文
2015/05/28 职场文书
平凡的世界读书笔记
2015/06/25 职场文书
家属联谊会致辞
2015/07/31 职场文书
Mac M1安装mnmp (Mac+Nginx+MySQL+PHP) 开发环境
2021/03/29 PHP
《杜鹃的婚约》OP主题曲「凸凹」无字幕影像公开
2022/04/08 日漫