使用workerman socket客户端同时连接多socket服务端问题

目前有一个功能需求想使用workerman实现

我采用workerman 作为客户端从其他服务端接收数据并处理,需要作为客户机同时连接两个不同的socket服务端,请问有方式实现吗?

981 2 0
2个回答

taozywu

如下Eg仅做参考:

<?php

require_once __DIR__ . './autoroad.php';

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

$worker1 = new Worker();
$worker1->onWorkerStart = function ($worker) {
        //global $con1, $con2;

        $con1 = new AsyncTcpConnection('tcp://127.0.0.1:4444');

        $con1->onConnect = function(AsyncTcpConnection $con) {
                $con->send('hello 444444');
        };

        $con1->onMessage = function(AsyncTcpConnection $con, $data) {
                //tcp://127.0.0.1:4444接收服务过来的数据
                echo $data;
                echo "!!!!!!!!!!!!!!";
        };

        $con1->connect();

        $con2 = new AsyncTcpConnection('tcp://127.0.0.1:5555');

        $con2->onConnect = function(AsyncTcpConnection $con) {
                $con->send('hello  55555555');
        };

        $con2->onMessage = function(AsyncTcpConnection $con, $data) {
                //tcp://127.0.0.1:5555接收服务过来的数据
                echo $data;
                echo "@@@@@@@@@@@@@";
        };

        $con2->connect();
};

$worker2 = new Worker("tcp://0.0.0.0:4444");
$worker2->onMessage = function($connection, $data)
{
    // 接收客户端过来的数据
    var_dump($data);
    //向客户端发送
    $connection->send('44444444444444 hello');
};

$worker3 = new Worker("tcp://0.0.0.0:5555");
$worker3->onMessage = function($connection, $data)
{
    // 接收客户端过来的数据
    var_dump($data);
    //向客户端发送
    $connection->send('55555555555 hello');
};

Worker::runAll();
年代过于久远,无法发表回答
×
🔝