温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Laravel中使用swoole实现websocket主动消息推送

发布时间:2021-05-14 17:13:41 来源:亿速云 阅读:469 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关怎么在Laravel中使用swoole实现websocket主动消息推送,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

主动消息推送实现

平常我们采用 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);   // 主动推送消息

Laravel 是什么

Laravel 是一套简洁、优雅的PHP Web开发框架。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。

看完上述内容,你们对怎么在Laravel中使用swoole实现websocket主动消息推送有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI