我正在尝试测试使用 NodeJS 与 PHP 通信的“最佳”解决方案,但我无法进行测试,因为我的循环出现了问题。
这是我的 NodeJS 代码:
var ENCODING = 'utf8';
// SOCKETS
var net = require('net'); // Load the TCP Library
// HTTP REQUEST
var http = require('http'); // Load the HTTP Library | WEBSOCKETS NEED THIS!!
var querystring = require('querystring'); // Load Query Library
// SOCKET SERVER
net.createServer(function (socket) {
socket.setEncoding(ENCODING);
socket.on('data', function (data) {
socket.end('TEXT FROM SOCKET', ENCODING);
});
socket.on('close', function () {
});
}).listen(5000);
// HTTP SERVER
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("TEXT FROM HTTP");
}).listen(2525);
这里是我的 PHP 测试:
set_time_limit(0);
$address = '127.0.0.1';
$response = null;
// METHOD: HTTP (2525)
$start = microtime();
for ($x=0; $x<1000; $x++) {
$response = getHttp($address);
}
echo "<br>METHOD 1: " . (microtime() - $start);
// METHOD: Open + Loop Send + Close (5000)
$start = microtime();
$sc = SockOpen($address);
for ($x=0; $x<1000; $x++) {
$response = SockWrite($sc, $address);
}
SockClose($sc);
echo "<br>METHOD 2: " . (microtime() - $start);
// MMETHOD: Loop (Open + Send + Close) (5000)
$start = microtime();
for ($x=0; $x<1000; $x++) {
$sc = SockOpen($address);
$response = SockWrite($sc, $address);
SockClose($sc);
}
echo "<br>METHOD 3: " . (microtime() - $start);
function SockOpen($address) {
ob_implicit_flush();
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
return false;
}
$result = socket_connect($socket, $address, 5000);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
return false;
}
return $socket;
}
function SockWrite($socket, $address) {
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: ".$address."\r\n";
$in .= "Connection: Close\r\n\r\n";
//$in = "key1:value1\n";
socket_write($socket, $in, strlen($in));
$buffer=null;
while ($out = socket_read($socket, 2048)) {
$buffer .= $buffer;
}
return $buffer;
}
function SockClose($socket) {
socket_close($socket); die();
}
function getHttp($address) {
$url = 'http://'.$address.':2525/';
$data = array('key1' => 'value1');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: text/plain\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
一个简单的连接工作正常,但如果我创建一个循环,套接字“断开”连接并且循环失败。失败发生在 HTTP 和 Socket 方法中。
知道如何解决这个问题吗?
谢谢