溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么在Laravel中使用swoole實(shí)現(xiàn)websocket主動(dòng)消息推送

發(fā)布時(shí)間:2021-05-14 17:13:41 來(lái)源:億速云 閱讀:483 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)怎么在Laravel中使用swoole實(shí)現(xiàn)websocket主動(dòng)消息推送,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

主動(dòng)消息推送實(shí)現(xiàn)

平常我們采用 swoole 來(lái)寫(xiě) WebSocket 服務(wù)可能最多的用到的是open,message,close這三個(gè)監(jiān)聽(tīng)狀態(tài),但是萬(wàn)萬(wàn)沒(méi)有看下下面的onRequest回調(diào)的使用,沒(méi)錯(cuò),解決這次主動(dòng)消息推送的就是需要用onRequest回調(diào)。

官方文檔:正因?yàn)閟woole_websocket_server繼承自swoole_http_server,所以在 websocket 中有onRequest回調(diào)。

詳細(xì)實(shí)現(xiàn)

# 這里是一個(gè)laravel中Commands
# 運(yùn)行php artisan swoole start 即可運(yùn)行
<?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;
  }
 }

 /**
  * 啟動(dòng)Swoole
  */
 private function start()
 {
  $this->ws = new swoole_websocket_server("0.0.0.0", 9502);
  //監(jiān)聽(tīng)WebSocket連接打開(kāi)事件
  $this->ws->on('open', function ($ws, $request) {
  });
  //監(jiān)聽(tīng)WebSocket消息事件
  $this->ws->on('message', function ($ws, $frame) {
   $this->info("client is SendMessage\n");
  });
  //監(jiān)聽(tīng)WebSocket主動(dòng)推送消息事件
  $this->ws->on('request', function ($request, $response) {
   $scene = $request->post['scene'];  // 獲取值
   $this->info("client is PushMessage\n".$scene);
  });
  //監(jiān)聽(tīng)WebSocket連接關(guān)閉事件
  $this->ws->on('close', function ($ws, $fd) {
   $this->info("client is close\n");
  });
  $this->ws->start();
 }
}

前面說(shuō)的是 swoole 中onRequest的實(shí)現(xiàn),下面實(shí)現(xiàn)下在控制器中主動(dòng)觸發(fā)onRequest回調(diào)。實(shí)現(xiàn)方法就是我們熟悉的curl請(qǐng)求。

# 調(diào)用activepush方法以后,會(huì)在cmd中打印出 
# client is PushMessage 主動(dòng)推送消息 字眼
 /**
  * CURL請(qǐng)求
  * @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);
 }
 
 /**
  * 主動(dòng)觸發(fā)
  */
 public function activepush()
 {
  $param['scene'] = '主動(dòng)推送消息';
  $this->curl($param);   // 主動(dòng)推送消息

Laravel 是什么

Laravel 是一套簡(jiǎn)潔、優(yōu)雅的PHP Web開(kāi)發(fā)框架。它可以讓你從面條一樣雜亂的代碼中解脫出來(lái);它可以幫你構(gòu)建一個(gè)完美的網(wǎng)絡(luò)APP,而且每行代碼都可以簡(jiǎn)潔、富于表達(dá)力。

看完上述內(nèi)容,你們對(duì)怎么在Laravel中使用swoole實(shí)現(xiàn)websocket主動(dòng)消息推送有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI