webman使用channel做进程间的通信,如何保障连接的publish,自己订阅不消费

zh7314
<?php

namespace process;

use Webman\Channel\Client;
use Workerman\Connection\TcpConnection;
use Workerman\Timer;

class Luckysheet
{
    public function __construct()
    {
    }
    //protected $connections = [];

    protected string $uid = '';

    public function onConnect(TcpConnection $connection)
    {
        $this->uid = uniqid();
        echo "onConnect\n";
    }

    public function onWebSocketConnect(TcpConnection $connection, $http_buffer)
    {
        echo "onWebSocketConnect\n";
        $data1 = '{"message":"连接成功","type":"0","status":"0"}';
        $connection->send($data1);
        $data2 = '{"data":"","message":"反馈以前操作信息","type":"4","status":"0"}';
        $connection->send($data2);
    }

    public function onMessage(TcpConnection $connection, $data)
    {
        $return = '{"createTime":1661484194207,"returnMessage":"success","type":1,"status":"0"}';
        if ($data == 'rub') {
            //心跳不回复
        } else {
            Client::connect('127.0.0.1', 2206);
            // 要订阅的事件名称(名称可以为任意的数字和字符串组合)
            $event_name = 'event_luckysheet';
            // 发布某个自定义事件,订阅这个事件的客户端会收到事件数据,并触发客户端对应的事件回调
            Client::publish($event_name, $data);
            // 订阅某个自定义事件并注册回调,收到事件后会自动触发此回调
            Client::on($event_name, function ($data) use ($connection) {
                // var_dump($data);
                // $connection->send($data . '1111111111111111111');
            });
            $connection->send($return);
        }
    }

    public function onClose(TcpConnection $connection)
    {
        //断开关闭句柄
        //        unset($this->connections[$connection->id]);
        echo "onClose\n";
    }
}

基于luckysheet做一个webman的后台,但是Channel进行进程间的通信,自己接受的data,返回
{"createTime":1661484194207,"returnMessage":"success","type":1,"status":"0"}
然后吧接口的数据分发给其他连接端,是需要模仿gateway自己维护一个uid,带到publish的$data里面
然后on的时候,在判断是否是自己发送出去的,还是有什么别的方案?

当连接进来的客户端发送请求的时候,

868 1 0
1个回答

walkor 打赏

自己判断下吧,没有更简单的方案

  • 暂无评论
年代过于久远,无法发表回答
×
🔝