我在laravel命令文件中创建了服务器&在监控器中设置为连续运行套接字服务器以接受客户端msg
Laravel命令文件代码Server.php
$loop = React\EventLoop\Factory::create();
$IP = getHostByName(getHostName()); // this will get current server IP address // 192.168.0.50
$socket = new React\Socket\Server($IP.':8080', $loop);
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
$connection->on('data', function ($data) use ($connection) {
// process data sent from client
});
});
$loop->run();Client.php
$loop = React\EventLoop\Factory::create();
$connector = new React\Socket\Connector($loop);
$connector->connect('192.168.0.50:8080')->then(function (React\Socket\ConnectionInterface $connection) use ($loop,$data) {
$connection->write($data); // sent data to Server.php
});
$loop->run();这很好,但是当我第二天检查时,它将从Client.php发送数据,但没有在Server.php中接收,然后重新启动Server.php / php手工服务器的监控程序,然后一整天都正常工作。
发布于 2021-07-01 05:12:45
我发现了到底发生了什么。
我已经将IP与端口192.168.0.50:8080设置为与服务器通信。但是第二天检查了IP更改为127.0.0.1:8080.。
下面是与任何IP地址通信的解决方案。Server.php
$loop = React\EventLoop\Factory::create();
$IP = getHostByName(getHostName()); // this will get current server IP address // 192.168.0.50
$IP = '0.0.0.0'; -> set this to I/O to any IP address
$socket = new React\Socket\Server($IP.':8080', $loop);
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
$connection->on('data', function ($data) use ($connection) {
// process data sent from client
});
});
$loop->run();sudo lsof -i -P -n | grep LISTEN -> run this cmd to check 8080 port
更新后的代码显示了现在的*:8080
https://stackoverflow.com/questions/68193912
复制相似问题