\Web\login.php里用new AsynTcpConnection 連接一個worker服務(wù)器,
worker服務(wù)器用這條連接返回服務(wù)器的數(shù)據(jù)后,login.php這邊要顯示。這種業(yè)務(wù)如何處理
如果用的是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();
};
onMessage($conn, $data)是你寫好的。我在里面加個
$_SERVER['HTTP_CONNECTION'] = $conn;
然后我改你 $remote_connection->httpConnection = $_SERVER['HTTP_CONNECTION'] ;
$remote_connection->httpConnection->send('xxxx');
這么 說來 上面的onMessage($conn, $data) 又會再收到'xxxx' 這個數(shù)據(jù)。
我想把這個xxxx數(shù)據(jù)也響應(yīng)到原來瀏覽器請求的Web/*.php里去。
$remote_connection->httpConnection->close("xxxx");
$remote_connection->httpConnection->send("xxxx");
$remote_connection->httpConnection->close();
這兩個是一樣的吧?(我用close("xxx") 這個,瀏覽器并沒收到xxxx)
這個是個可運行的例子,你看下
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();
}
你這個是可以收到的。我去var_dump( $remote_connection->httpConnection)
它的_status值是8 這是被關(guān)掉的連接。所以瀏覽器不會收到了。
因為你的webServer里的onMessage在處理一個*.php頁面后,會把這個connection關(guān)掉。
$content = ob_get_clean();
ini_set('display_errors', 'on');
// $connection->close($content); 我把這個注釋掉后。瀏覽器收到數(shù)據(jù)了。。。。
chdir($cwd);
WebServer不能這么干。webserver直接用阻塞socket去發(fā)吧
$sock = stream_socket_client('tcp://xxxx:xx');
fwrite($sock, 'data');
$result = fread($sock, 8192);
根據(jù)協(xié)議讀取,比如json+回車,判斷最后一個字符是否是回車,是回車就讀完,不是的話繼續(xù)讀,讀的時候用feof判斷下是否已經(jīng)斷開,斷開也就不讀了
@1:如果這個協(xié)議數(shù)據(jù),一次沒讀完。我如何再讀,因為你這個$length的長度寫好了,
是不是再判定完未完成里再fread($socket, $length)
然后把fread兩次的數(shù)據(jù)合并起來
@1:
XoxServer::LOG_DEBUG("web2login link count{$count}",__FILE__, __LINE__);
這是我寫的調(diào)試log的。有沒有辦法不用每次__FILE__ __LINE
我問我另一個老同事,它說用try catch。你們是用哪種方式來做臨時調(diào)試的
@1:每次調(diào)用LOG_DEBUG()這個靜態(tài)方法都要自已加__FIILE__ LINE很麻煩。
有沒有XoxServer::LOG_DEBUG("web2login link count{$count}")這樣子就可以的。