我试图部署我的websocket服务器并开始运行它但总是给出:
PHP Fatal error:
Uncaught exception 'React\Socket\ConnectionException'
with message 'Could not bind to tcp://my_ip_here:8080:
Address already in use'
in /var/www/html/webscoket/vendor/react/socket/src/Server.php:29
这是我的server.php:
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use React\Socket\Server;
use React\ZMQ\Context;
$loop = React\EventLoop\Factory::create();
$app = new onyxsocket();
$webSock = new Server($loop);
$webSock->listen(8080, 'my_ip_here');
$webServer = new IoServer(
new HttpServer(
new WsServer(
$app
)
),
$webSock
);
$context = new Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://my_ip_here:5555');
$pull->on('error', function ($e) {
var_dump($e->getMessage());
});
$pull->on('message', array($app, 'onbroadcast'));
$loop->run();
到目前为止我所尝试的是检查可以在生产服务器中使用的可用端口:netstat – anp让我知道端口8080是免费的.但问题是它仍然显示已经使用的错误地址.我也尝试过管理员提供的其他端口,但没有运气.
我正在尝试部署的server.php在localhost上工作正常.但我不知道我需要做什么才能使它在生产服务器上运行.
需要帮忙.谢谢.
解决方法:
来自@ user3666197评论如上:
Clarify the code, pls. Your server-code ZeroMQ .bind() -s to port# 5555. So whose code binds to localhost port# 8080, that is reported in an unhandled exception above? How do you clean for a gracefull-exit any crashed EventLoop/Factory to release resources and avoid hanging orphans?
我决定重新检查netstat:netstat -tulpen并检查端口8080和5555,并发现当前连接的端口上存在已注册的PID.这些应用程序也是我想在控制台server.php上运行的相同脚本.
我杀了PID:kill PID_number并再次在控制台上运行server.php.有效.