您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman常见问题-监听ipv6
发布时间:2022-01-04 23:03:37编辑:雪饮阅读()
问:如何让客户端即能通过ipv4地址访问,也能通过ipv6地址访问?
答:在初始化容器的时候监听地址写[::]即可。
实例:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://[::]:8080');
$worker->onMessage=function(TcpConnection $connection){
$connection->send("this is ipv6 http");
};
Worker::runAll();
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://[::]:8080');
$worker->onMessage=function(TcpConnection $connection){
$connection->send("this is ipv6 http");
};
Worker::runAll();
实例运行:
[root@localhost workerman]# php index.php start
Workerman[index.php] start in DEBUG mode
--------------------------------------- WORKERMAN ----------------------------------------
Workerman version:4.0.26 PHP version:7.3.31
---------------------------------------- WORKERS -----------------------------------------
proto user worker listen processes status
tcp root none http://[::]:8080 1 [OK]
------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
根据实例运行后的监听地址(也就是代码中的监听地址)
则可以用elinks访问本地ipv6:
[root@localhost ~]# elinks http://[::]:8080 --dump
this is ipv6 http
也可以查看当前服务器的ipv6地址
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.53 netmask 255.255.255.0 broadcast 192.168.31.255
inet6 fe80::d10a:ecc2:4401:e47e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d8:c6:af txqueuelen 1000 (Ethernet)
RX packets 569942 bytes 76412341 (72.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 520495 bytes 581618944 (554.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 5095 bytes 1471080 (1.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5095 bytes 1471080 (1.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
然后浏览器访问如:
http://[fe80::d10a:ecc2:4401:e47e]:8080/
关键字词:workerman,常见问题,监听ipv6,ipv6