打洞其实很简单,写一个打洞服务器,然后客户端把自己路由器的ip和端口注册到打洞服务器上,然后打洞服务器去告诉另一个客户端,希望交流的客户端路由器的ip和端口,因为路由器有NAT和NATP,对外暴露的ip和端口不一定是本机的,在这里主要做本地的模拟,线上的其实也很简单,tcp与udp都可以打洞
通讯框架用的swoole,通讯技术和语言没有任何关系,不要纠结语言
一个打洞服务器:
<?php
$arr = [];
$server = new Swoole\Server('127.0.0.1', 9503, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
$server->set([
"worker_num" => 1
]);
$server->on('packet', function ($server, $data, $clientInfo) use (&$arr){
$list = explode("\n", $data);
foreach ($list as $value) {
$data = json_decode($value, 1);
if ($data) {
if ($data["do"] == "reg") {
$arr[$data["user"]] = $clientInfo;
var_dump($arr);
$server->sendTo($clientInfo['address'], $clientInfo['port'], "Server:");
// $server->send($fd, "ok\n");
} else if ($data["do"] == "get") {
$server->sendTo($clientInfo['address'], $clientInfo['port'],
json_encode($arr[$data["user"]]));
// $server->send($fd, json_encode($arr[$data["user"]]));
//
// $server->send($arr[$data["user"]]["fd"], json_encode($arr[$data["user"]]));
} else {
}
}
}
//$server->sendTo($clientInfo['address'], $clientInfo['port'], "Server:{$data}");
});
$server->start();
client2:
<?php
$client = new Swoole\Client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_SYNC);
$r = $client->sendto('127.0.0.1', 9503, json_encode(["do"=>"reg", "user"=>"client2", "passwd" => "passwd"]) . "\n");
$ret = $client->recv();
while (1) {
$ret = $client->recv();
if ($ret) {
var_dump($ret);
}
sleep(1);
}
$client->close();
client1:
<?php
$client = new Swoole\Client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_SYNC);
$r = $client->sendto('127.0.0.1', 9503, json_encode(["do"=>"reg", "user"=>"client1", "passwd" => "passwd"]) . "\n");
echo $client->recv();
$r = $client->sendto('127.0.0.1', 9503, json_encode(["do"=>"get", "user"=>"client2"]) . "\n");
$ret = $client->recv();
$data = json_decode($ret, 1);
while (1) {
$r = $client->sendto($data["address"], $data["port"], "test" . "\n");
var_dump($r);
sleep(3);
}
运行这些程序我们发现,swoole的client2 收到了test的数据
当然线上的程序有路由表缓存策略,还要再做处理,tcp配置一下SO_REUSEADDR其实也可以了,就是这么简单
|