laraval6.0 GatewayWorker 交互通信

本文介绍了如何在Laravel6.0项目中集成GatewayWorker和GatewayClient,包括下载、前端测试、用户绑定功能的实现以及消息推送的API调用。开发者需配置GatewayClient并确保正确连接到GatewayWorker服务器。
摘要由CSDN通过智能技术生成

开发前准备

GatewayClient 官网:https://www.workerman.net/
当前使用的是宝塔操作

下载 GatewayWorker 及操作方式

在这里插入图片描述

前端demo

在这里插入图片描述

测试效果

在这里插入图片描述

项目中安装GatewayClient

在这里插入图片描述

composer require workerman/gatewayclient

在这里插入图片描述
绑定用户 和 消息推送

<?php


namespace App\Http\Controllers\Common;

use App\Exceptions\Exception\ErrorException;
use GatewayClient\Gateway;

require_once '../vendor/workerman/gatewayclient/Gateway.php';

class Push
{

    public function __construct()
    {
        //注意:127.0.0.1:1238 的1238端口不是随意填写的 是在GatewayWorker/yourApp/start_gateway.php 里面
        //$gateway->registerAddress = '127.0.0.1:1238'; 配置的
        Gateway::$registerAddress = '127.0.0.1:1238';
    }


    /**
     * Notes:  绑定用户
     * Author: Seven
     * Method: getUserBinding
     * Date: 2023/11/29: 17:27
     * @param $client_id
     * @param $users_id
     */
    public function getUserBinding($param)
    {
        if(empty($param['client_id'])) throw new ErrorException('缺少 client_id');
        if(empty($param['users_id'])) throw new ErrorException('缺少 users_id');
        Gateway::bindUid($param['client_id'],$param['users_id']);
        dd($param['client_id']);
    }


    /**
     * Notes:  推送信息
     * Author: Seven
     * Method: getSendPush
     * Date: 2023/11/29: 17:42
     * @param $users_id
     * @param $content
     */
    public function getSendPush($users_id,$type,$content){
        $data = [
            'type'=>$type,
            'msg'=>$content,
        ];
        Gateway::sendToUid($users_id,json_encode($data));
    }


}

查看结果
调用推送的接口
在这里插入图片描述
到这里说明已经交互成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值