WebServer new AsynTcpConnection连接Worker

wuchuguang

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

3122 2 0
2个回答

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();
};
  • wuchuguang 2015-05-20

    $http_worker = new Worker('http://0.0.0.0:80');这个我是用你的WebServer

    onMessage($conn, $data)是你写好的。我在里面加个
    $_SERVER['HTTP_CONNECTION'] = $conn;

    然后我改你 $remote_connection->httpConnection = $_SERVER['HTTP_CONNECTION'] ;

    $remote_connection->httpConnection->send('xxxx');
    这么 说来 上面的onMessage($conn, $data) 又会再收到'xxxx' 这个数据。
    我想把这个xxxx数据也响应到原来浏览器请求的Web/*.php里去。

  • wuchuguang 2015-05-20

    我想把这个xxxx数据也响应到原来浏览器请求的Web/*.php里去。 这个有可能实现到的吗?

  • walkor 2015-05-20

    {{{
    $remote_connection->httpConnection->send('xxxx');
    }}}
    是发送给浏览器

  • wuchuguang 2015-05-20

    $remote_connection->httpConnection->close("xxxx");

    $remote_connection->httpConnection->send("xxxx");
    $remote_connection->httpConnection->close();

    这两个是一样的吧?(我用close("xxx") 这个,浏览器并没收到xxxx)

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();
}
  • wuchuguang 2015-05-20

    你这个是可以收到的。我去var_dump( $remote_connection->httpConnection)
    它的_status值是8 这是被关掉的连接。所以浏览器不会收到了。
    因为你的webServer里的onMessage在处理一个*.php页面后,会把这个connection关掉。

  • wuchuguang 2015-05-20

    $content = ob_get_clean();
    ini_set('display_errors', 'on');
    // $connection->close($content); 我把这个注释掉后。浏览器收到数据了。。。。
    chdir($cwd);

  • walkor 2015-05-20

    WebServer不能这么干。webserver直接用阻塞socket去发吧

    $sock = stream_socket_client('tcp://xxxx:xx');
    fwrite($sock, 'data');
    $result = fread($sock, 8192);

  • wuchuguang 2015-05-20

    @1: TcpConnection只能做服务端是吧。 你有没有个同步版的TCP客户端?

  • walkor 2015-05-20

    用stream_socket_client就可以

  • wuchuguang 2015-05-20

    @1:读解协议,解析协议没你这个好用。

  • wuchuguang 2015-05-20

    @1:fread($socket, $length)这个长度不知道。读长了不好,读短了不好

  • walkor 2015-05-20

    根据协议读取,比如json+回车,判断最后一个字符是否是回车,是回车就读完,不是的话继续读,读的时候用feof判断下是否已经断开,断开也就不读了

  • wuchuguang 2015-05-20

    @1:如果这个协议数据,一次没读完。我如何再读,因为你这个$length的长度写好了,
    是不是再判定完未完成里再fread($socket, $length)
    然后把fread两次的数据合并起来

  • wuchuguang 2015-05-20

    @1:
    XoxServer::LOG_DEBUG("web2login link count{$count}",__FILE__, __LINE__);
    这是我写的调试log的。有没有办法不用每次__FILE__ __LINE
    我问我另一个老同事,它说用try catch。你们是用哪种方式来做临时调试的

  • walkor 2015-05-20

    __FILE__, __LINE__ 很好用啊,为什么不用

  • wuchuguang 2015-05-21

    @1:每次调用LOG_DEBUG()这个静态方法都要自已加__FIILE__ LINE很麻烦。
    有没有XoxServer::LOG_DEBUG("web2login link count{$count}")这样子就可以的。

  • walkor 2015-05-21

    debug_backtrace 看下这个函数,应该能帮到你

年代过于久远,无法发表回答
🔝