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"
        ]
    ]
);

四、提前说明

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

相关推荐
无心水14 小时前
【任务调度:数据库锁 + 线程池实战】1、多节点抢任务?SELECT FOR UPDATE SKIP LOCKED 才是真正的无锁调度神器
人工智能·分布式·后端·微服务·架构
сокол14 小时前
【网安-Web渗透测试-漏洞系列】逻辑漏洞(或越权漏洞)
web安全·php
only-qi17 小时前
ZAB 协议深度解析:ZooKeeper 分布式一致性的核心
分布式·zookeeper·zab
сокол18 小时前
【网安-Web渗透测试-漏洞系列】XXE漏洞
xml·web安全·php
014-code18 小时前
Redis 分布式锁:从 0 到 1 完整演变
数据库·redis·分布式
白太岁19 小时前
Redis:(5) 分布式锁实现:原子性设置锁与 Lua 释放锁
数据库·redis·分布式
闲人编程19 小时前
定时任务与周期性调度
分布式·python·wpf·调度·cron·定时人物·周期性
Coder_Boy_20 小时前
Java高级_资深_架构岗 核心知识点全解析(模块四:分布式)
java·spring boot·分布式·微服务·设计模式·架构
Francek Chen21 小时前
【大数据存储与管理】分布式文件系统HDFS:03 HDFS的相关概念
大数据·hadoop·分布式·hdfs
利刃大大21 小时前
【SpringCloud】网关GateWay && Spring Cloud Gateway && Route Predicate Factories
网关·spring·spring cloud·gateway