实现websocket 主动消息推送,用laravel+Swoole - 八重樱 - 博客园


本站和网页 https://www.cnblogs.com/a609251438/archive/2019/11/23/11917708.html 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

实现websocket 主动消息推送,用laravel+Swoole - 八重樱 - 博客园
首页
新闻
博问
专区
闪存
班级
我的博客
我的园子
账号设置
简洁模式 ...
退出登录
注册
登录
Raylove
博客园
首页
新随笔
联系
订阅
管理
实现websocket 主动消息推送,用laravel+Swoole
近来有个需求:想实现一个可以主动触发消息推送的功能,这个可以实现向模板消息那个,给予所有成员发送自定义消息,而不需要通过客户端发送消息,服务端上message中监听传送的消息进行做相对于的业务逻辑。
主动消息推送实现平常我们采用 swoole 来写 WebSocket 服务可能最多的用到的是open,message,close这三个监听状态,但是万万没有看下下面的onRequest回调的使用,没错,解决这次主动消息推送的就是需要用onRequest回调。官方文档:正因为swoole_websocket_server继承自swoole_http_server,所以在 websocket 中有onRequest回调。
详细实现:
1 # 这里是一个laravel中Commands
2 # 运行php artisan swoole start 即可运行
3 <?php
5 namespace App\Console\Commands;
7 use Illuminate\Console\Command;
8 use swoole_websocket_server;
10 class Swoole extends Command
11 {
12 public $ws;
13 /**
14 * The name and signature of the console command.
15 *
16 * @var string
17 */
18 protected $signature = 'swoole {action}';
19
20 /**
21 * The console command description.
22 *
23 * @var string
24 */
25 protected $description = 'Active Push Message';
26
27 /**
28 * Create a new command instance.
29 *
30 * @return void
31 */
32 public function __construct()
33 {
34 parent::__construct();
35 }
36
37 /**
38 * Execute the console command.
39 *
40 * @return mixed
41 */
42 public function handle()
43 {
44 $arg = $this->argument('action');
45 switch ($arg) {
46 case 'start':
47 $this->info('swoole server started');
48 $this->start();
49 break;
50 case 'stop':
51 $this->info('swoole server stoped');
52 break;
53 case 'restart':
54 $this->info('swoole server restarted');
55 break;
56 }
57 }
58
59 /**
60 * 启动Swoole
61 */
62 private function start()
63 {
64 $this->ws = new swoole_websocket_server("0.0.0.0", 9502);
65 //监听WebSocket连接打开事件
66 $this->ws->on('open', function ($ws, $request) {
67 });
68 //监听WebSocket消息事件
69 $this->ws->on('message', function ($ws, $frame) {
70 $this->info("client is SendMessage\n");
71 });
72 //监听WebSocket主动推送消息事件
73 $this->ws->on('request', function ($request, $response) {
74 $scene = $request->post['scene']; // 获取值
75 $this->info("client is PushMessage\n".$scene);
76 });
77 //监听WebSocket连接关闭事件
78 $this->ws->on('close', function ($ws, $fd) {
79 $this->info("client is close\n");
80 });
81 $this->ws->start();
82 }
83 }
前面说的是 swoole 中onRequest的实现,下面实现下在控制器中主动触发onRequest回调。实现方法就是我们熟悉的curl请求。
1 # 调用activepush方法以后,会在cmd中打印出
2 # client is PushMessage 主动推送消息 字眼
3 /**
4 * CURL请求
5 * @param $data
6 */
7 public function curl($data)
8 {
9 $curl = curl_init();
10 curl_setopt($curl, CURLOPT_URL, "http://127.0.0.1:9502");
11 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
12 curl_setopt($curl, CURLOPT_HEADER, 1);
13 curl_setopt($curl, CURLOPT_POST, 1);
14 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
15 curl_exec($curl);
16 curl_close($curl);
17 }
18
19 /**
20 * 主动触发
21 */
22 public function activepush()
23 {
24 $param['scene'] = '主动推送消息';
25 $this->curl($param); // 主动推送消息
用途onRequest 回调特别适用于需要在控制器中调用的推送消息,比如模板消息之类,在控制器中调用。
推荐阅读:
PHP 当Swoole 遇上 ThinkPHP5
Swoole和Redis实现的并发队列处理系统
PHP laravel+thrift+swoole打造微服务框架
PHP Swoole-Demo TCP服务端简单实现
PHP Swoole长连接常见问题
PHP+Swoole并发编程的魅力
php Swoole实现毫秒级定时任务
Swoole跟thinkphp5结合开发WebSocket在线聊天通讯系统
很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的加群(点击→)677079770
posted @
2019-11-23 13:54
八重樱
阅读(4835)
评论(2)
编辑
收藏
举报
刷新评论刷新页面返回顶部
Copyright 2022 八重樱
Powered by .NET 7.0 on Kubernetes