workerman怎么实现p2p,通过公网服务器辅助之后,客户端直连

bogeStore

问题描述

workerman怎么实现p2p,通过公网服务器辅助之后,客户端直连。既网上说的udp打动

为此你搜索到了哪些方案及不适用的原因

我根据开发手册的udp的案例,服务器和客户端是可以udp通讯的,但是,拿到对应的ip和port后,客户端之间就是无法通讯,请问有大神指点一下吗

服务器端源码

$http_worker = new Worker("udp://0.0.0.0:9999");
$http_worker->name = "udp_char";
$http_worker->data = ['pc1'=>'', 'pc2'=>''];
$http_worker->count = 1;
// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function($connection, $request) use($http_worker)
{
    $request = json_decode($request, true);
    $http_worker->data[$request['from']] = $connection->getRemoteIp() . ':' . $connection->getRemotePort();

    $data = ['code'=>0, 'socket'=>''];
    if ($http_worker->data[$request['to']]) {
        $data['code'] = 1;
        $data['socket'] = $http_worker->data[$request['to']];
    }
    $connection->send(json_encode($data));
};

Worker::runAll();

客户端的源码:

$http_worker = new Worker("udp://0.0.0.0:0");
$http_worker->name = "udp_char";
$http_worker->count = 1;
$http_worker->onWorkerStart = function($worker)
{
    // 2秒后启动一个udp客户端,连接
    Timer::add(2, function(){
        $udp_connection = new AsyncUdpConnection('udp://server:9999');
        $udp_connection->onConnect = function(AsyncUdpConnection $udp_connection){
            $udp_connection->time_id = Timer::add(5, function()use($udp_connection){
                $data = ["to"=>"pc2", "from"=>"pc1"];
                $udp_connection->send(json_encode($data));
            });
        };
        $udp_connection->onMessage = function(AsyncUdpConnection $udp_connection, $request){
            // 收到服务端返回的数据
            $request = json_decode($request, true);
            if ($request['code']) {
                echo "\t\n------------ 访问pc2 ----------------\t\n";
                $client = stream_socket_client('udp://' . $request['socket']);
                $data = ["code"=> 1, "msg"=>"wo shi pc1"];
                fwrite($client, json_encode($data));
                stream_socket_sendto($client, "udp数据");

                $udp_connection_pc = new AsyncUdpConnection('udp://'.$request['socket']);

                $udp_connection_pc->onConnect = function(AsyncUdpConnection $udp_connection_pc){
                    echo "\t\n连接\t\n";
                    $udp_connection_pc->time_id = Timer::add(5, function()use($udp_connection_pc){
                        echo "\t\n发数据给pc2\t\n";
                        $data = ["code"=> 1, "msg"=>"wo shi pc1"];
                        $udp_connection_pc->send(json_encode($data));
                    });
                };

                $udp_connection_pc->onMessage = function(AsyncUdpConnection $udp_connection_pc, $request){
                    // 收到服务端返回的数据
                    var_dump($request);

                };
                $udp_connection_pc->connect();

                Timer::del($udp_connection->time_id);

            }
        };

        $udp_connection->connect();
    }, null, false);
};

$http_worker->onMessage = function($connection, $request)
{
    $data = ["code"=> 1, "msg"=>"wo shi pc1"];
    $udp_connection->send(json_encode($data));
};
Worker::runAll();

求助各路大神,指点指点

906 2 0
2个回答

ysxpark

udp打洞,我也试过,但没成功,蹲一下别的大神

  • 暂无评论
缝合

不太好成功,局域网打洞还容易些,公网不太容易。之前打了半天,最后查的是有很多途径不让你打洞囧

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