workman服务端开发模式-应用开发-后端api推送工具开发

一、安装workerman/gatewayclient

复制代码
composer require workerman/gatewayclient

二、工具集开发

在根目录下extend文件夹下创建文件夹并命名为Workman,在Woekman文件夹下创建推送工具并命名为GatewayWork.php,代码如下:

复制代码
<?php
/**
 * 长链接发送信息
 * User: 龙哥 三年风水
 * Date: 2024/12/21/0021
 * Time: 22:52
 */
namespace Workman;
use GatewayClient\Gateway;
class GatewayWork
{
    //向客户端发送内容
    public static function sendToClient($client_id, $data){
        //获取配置文件
        $isFormal = config('socket.is_formal');
        Gateway::$registerAddress = config("socket.tcp_address.{$isFormal}.address");
        Gateway::sendToClient($client_id, $data);
    }

    //向群推送数据
    public static function sendToGroup($group,$message){
        //获取配置文件
        $isFormal = config('socket.is_formal');
        Gateway::$registerAddress = config("socket.tcp_address.{$isFormal}.address");
        Gateway::sendToGroup($group,json_encode($message));
    }

    //批量向客户端发送内容
    public static function sendToAll($message, $client_id_array = null){
        //获取配置文件
        $isFormal = config('socket.is_formal');
        Gateway::$registerAddress = config("socket.tcp_address.{$isFormal}.address");
        Gateway::sendToAll(json_encode($message), $client_id_array);
    }

    //获取与 uid 绑定的 client_id 列表
    public static function getClientIdByUid($uid){
        //获取配置文件
        $isFormal = config('socket.is_formal');
        Gateway::$registerAddress = config("socket.tcp_address.{$isFormal}.address");
        return Gateway::getClientIdByUid($uid);
    }

    //关闭客户端
    public static function onClose($userId){
        //获取配置文件
        $isFormal = config('socket.is_formal');
        Gateway::$registerAddress = config("socket.tcp_address.{$isFormal}.address");
        $resUser = Gateway::isUidOnline((int)$userId);
        if($resUser == 1){
            $resClientUser = Gateway::getClientIdByUid((int)$userId);
            foreach ($resClientUser as $v){
                Gateway::sendToClient($v, json_encode(['type' => 'close','data' => 'ok']));
                Gateway::destoryClient($v);
            }
        }
    }
}

三、创建Gateway配置文件

在根目录下config文件夹下新建文件并命名为socket.php,代码如下:

复制代码
<?php
/**
 * 长链接配置文件
 * User: 龙哥 三年风水
 * Date: 2024/12/21/0021
 * Time: 22:56
 */
return array(
    'is_formal' =>  '1',//正式环境时修改为0,测试环境时修改为1 本地环境时修改为2
    'tcp_address' => [
        [
            'address'    =>  "172.30.7.98:1236"
        ],
        [
            'address'    =>  "172.20.36.144:1239"
        ],
        [
            'address'    =>  "0.0.0.0:1236"
        ]
    ]
);

四、提前说明

明天测试及优化,时间允许的情况下,开始前后端连调。

相关推荐
你我约定有三2 小时前
软件启动时加配置文件 vs 不加配置文件
java·分布式·zookeeper
励志成为糕手4 小时前
ZooKeeper架构深度解析:分布式协调服务的核心设计与实现
大数据·分布式·zookeeper·架构·一致性
瑞瑞绮绮6 小时前
分布式事务的Java实践
java·分布式·springcloud
孤独的复仇者6 小时前
RabbitMQ高级:延迟消息
分布式·rabbitmq
魔道不误砍柴功9 小时前
Mac 能够连Wife,但是不能上网问题解决
网络·macos·php
AMiner:AI科研助手9 小时前
警惕!你和ChatGPT的对话,可能正在制造分布式妄想
人工智能·分布式·算法·chatgpt·deepseek
码luffyliu12 小时前
消息队列 :Kafka 核心要点总结
分布式·kafka·消息队列·mq
搬码临时工16 小时前
怎样让外网计算机访问局域网计算机?通过公网地址访问不同内网服务的设置方法
开发语言·php
运维老司机17 小时前
Kafka 开启 SASL_PLAINTEXT 双监听器认证(内网/外网)
分布式·kafka·linq
fengfuyao98518 小时前
分布式电源接入电网进行潮流计算
分布式