百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>网页制作> 用laravel+Swoole实现websocket自动新闻推送
分享文章到:

用laravel+Swoole实现websocket自动新闻推送

发布时间:09/01 来源:未知 浏览: 关键词:
近来有个需求:想实现一个可以主动触发新闻推送的功效,这个可以实现向模板新闻阿谁,给予所有成员发送自定义新闻,而不需要通过客户端发送新闻,效劳端上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);            // 主动推送新闻

用处
onRequest 回调特殊适用于需要在操纵器中调取的推送新闻,比方模板新闻之类,在操纵器中调取。

更多PHP相关知识,请拜访PHP中文网!

以上就是用laravel+Swoole实现websocket主动新闻推送的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有153人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板