国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

WebServer new AsynTcpConnection連接Worker

wuchuguang

\Web\login.php里用new AsynTcpConnection 連接一個worker服務(wù)器,
worker服務(wù)器用這條連接返回服務(wù)器的數(shù)據(jù)后,login.php這邊要顯示。這種業(yè)務(wù)如何處理

3641 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獲得對應(yīng)的http_connection,向瀏覽器發(fā)送數(shù)據(jù)
        $remote_connection->httpConnection->send('xxxx');
        // 關(guān)閉不用的鏈接,避免內(nèi)存泄露
        $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獲得對應(yīng)的http_connection,向瀏覽器發(fā)送數(shù)據(jù)
        $remote_connection->httpConnection->close(htmlspecialchars($data));
        // 關(guān)閉不用的鏈接,避免內(nèi)存泄露
        $remote_connection->httpConnection = null;
        $remote_connection->close();
    };
    $remote_connection->connect();
};

Worker::runAll();
}
  • wuchuguang 2015-05-20

    你這個是可以收到的。我去var_dump( $remote_connection->httpConnection)
    它的_status值是8 這是被關(guān)掉的連接。所以瀏覽器不會收到了。
    因為你的webServer里的onMessage在處理一個*.php頁面后,會把這個connection關(guān)掉。

  • wuchuguang 2015-05-20

    $content = ob_get_clean();
    ini_set('display_errors', 'on');
    // $connection->close($content); 我把這個注釋掉后。瀏覽器收到數(shù)據(jù)了。。。。
    chdir($cwd);

  • walkor 2015-05-20

    WebServer不能這么干。webserver直接用阻塞socket去發(fā)吧

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

  • wuchuguang 2015-05-20

    @1: TcpConnection只能做服務(wù)端是吧。 你有沒有個同步版的TCP客戶端?

  • walkor 2015-05-20

    用stream_socket_client就可以

  • wuchuguang 2015-05-20

    @1:讀解協(xié)議,解析協(xié)議沒你這個好用。

  • wuchuguang 2015-05-20

    @1:fread($socket, $length)這個長度不知道。讀長了不好,讀短了不好

  • walkor 2015-05-20

    根據(jù)協(xié)議讀取,比如json+回車,判斷最后一個字符是否是回車,是回車就讀完,不是的話繼續(xù)讀,讀的時候用feof判斷下是否已經(jīng)斷開,斷開也就不讀了

  • wuchuguang 2015-05-20

    @1:如果這個協(xié)議數(shù)據(jù),一次沒讀完。我如何再讀,因為你這個$length的長度寫好了,
    是不是再判定完未完成里再fread($socket, $length)
    然后把fread兩次的數(shù)據(jù)合并起來

  • wuchuguang 2015-05-20

    @1:
    XoxServer::LOG_DEBUG("web2login link count{$count}",__FILE__, __LINE__);
    這是我寫的調(diào)試log的。有沒有辦法不用每次__FILE__ __LINE
    我問我另一個老同事,它說用try catch。你們是用哪種方式來做臨時調(diào)試的

  • walkor 2015-05-20

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

  • wuchuguang 2015-05-21

    @1:每次調(diào)用LOG_DEBUG()這個靜態(tài)方法都要自已加__FIILE__ LINE很麻煩。
    有沒有XoxServer::LOG_DEBUG("web2login link count{$count}")這樣子就可以的。

  • walkor 2015-05-21

    debug_backtrace 看下這個函數(shù),應(yīng)該能幫到你

年代過于久遠,無法發(fā)表回答
??