WebServer new AsynTcpConnection连接Worker

0

\Web\login.php里用new AsynTcpConnection 连接一个worker服务器,
worker服务器用这条连接返回服务器的数据后,login.php这边要显示。这种业务如何处理

已邀请:

walkor

赞同来自:

如果用的是Worker,代码类似下面


$http_worker = new Worker('http://0.0.0.0:80');
$http_worker->onMessage = function($http_connection, $data)
{
$remote_connection = new AsynTcpConnection('xxx://x.x.x.x:xx');
$remote_connection->send('xxx');
// 把http_connection保存到$remote_connection一个属性里面,属性名随意,这里叫httpConnection
$remote_connection->httpConnection = $http_connection;
$remote_connection->onMessage = function($remote_connection, $data)
{
// 通过remote_connection获得对应的http_connection,向浏览器发送数据
$remote_connection->httpConnection->send('xxxx');
// 关闭不用的链接,避免内存泄露
$remote_connection->httpConnection = null;
$remote_connection->close();
};
$remote_connection->connect();
};

walkor

赞同来自:

这个是个可运行的例子,你看下


require_once __DIR__.'/Workerman/Autoloader.php';
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

$http_worker = new Worker('http://0.0.0.0:1234');
$http_worker->onMessage = function($http_connection, $data)
{
$remote_connection = new AsyncTcpConnection('tcp://baidu.com:80');
$remote_connection->send("GET / HTTP/1.1\r\nHost: baidu.com\r\nConnection: Close\r\nCache-Control: max-age=0\r\nAccept: text/html\r\n\r\n");
// 把http_connection保存到$remote_connection一个属性里面,属性名随意,这里叫httpConnection
$remote_connection->httpConnection = $http_connection;
$remote_connection->onMessage = function($remote_connection, $data)
{
// 通过remote_connection获得对应的http_connection,向浏览器发送数据
$remote_connection->httpConnection->close(htmlspecialchars($data));
// 关闭不用的链接,避免内存泄露
$remote_connection->httpConnection = null;
$remote_connection->close();
};
$remote_connection->connect();
};

Worker::runAll();
}

要回复问题请先登录注册