2013-10-29 48 views
-2

我試圖測試「最佳」解決方案與NodeJS通信到PHP,但我無法做測試,因爲我使循環出錯。套接字和Http循環中斷

這是我的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方法發生失敗。

任何想法如何解決這個問題?

感謝

+0

這是如何被認爲是PHP和node.js之間進行通信的最佳解決方案? – Gntem

+0

因爲我在這個論壇上提出了一些問題來確定公共服務器中的快速解決方案是什麼(沒有redis,memcached,沒有更多的apache-PHP 5.2和NodeJS,所有的響應都是「套接字或者httprequest」)。找到禁食的方式。 – Zenth

回答

0

我更新代碼,搜索的錯誤,我發現在服務器&客戶端代碼的問題,這裏有updateds:

客戶:

set_time_limit(0); 
$address = '127.0.0.1'; 

$response = null; 

// METHOD: HTTP (2525) 
$start = microtime(); 
$fails = 0; 
for ($x=0; $x<1000; $x++) { 
    $response = getHttp($address); 
    if (empty($response)) { 
     $fails++; 
    } 
} 
echo "<br>METHOD 1: " . (microtime() - $start) . " errors: " . $fails; 


// METHOD: Open + Loop Send + Close (5000) 
$start = microtime(); 
$fails = 0; 
$sc = SockOpen($address); 
for ($x=0; $x<1000; $x++) { 
    $response = SockWrite($sc, $address); 
    if (empty($response)) { 
     $fails++; 
    } 
} 
SockClose($sc); 
echo "<br>METHOD 2: " . (microtime() - $start) . " errors: " . $fails; 


// MMETHOD: Loop (Open + Send + Close) (5000) 
$start = microtime(); 
$fails = 0; 
for ($x=0; $x<1000; $x++) { 
    $sc = SockOpen($address); 
    $response = SockWrite($sc, $address); 
    if (empty($response)) { 
     $fails++; 
    } 
    SockClose($sc); 
} 
echo "<br>METHOD 3: " . (microtime() - $start) . " errors: " . $fails; 



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"; 

    socket_write($socket, $in, strlen($in)); 

    $buffer=null; 
    //while ($out = socket_read($socket, 1024, MSG_WAITALL)) { 
    while (socket_recv($socket, $buffer, 2048, MSG_WAITALL) === true) { 
     $buffer .= $buffer; 
    } 

    return $buffer; 

} 

function SockClose($socket) { 
    socket_shutdown($socket); 
    socket_close($socket); 
} 

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); 
    try { 
     $result = @file_get_contents($url, false, $context); 
    } catch (Exception $e) { 
     return false; 
    } 

    return $result; 
} 

服務器:

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.write("TEXT FROM SOCKET", ENCODING); 

    socket.on('data', function (data) { 
    socket.write("TEXT FROM SOCKET\n", ENCODING); 
    //socket.end(); 
    }); 

    socket.on('close', function() { 

    }); 

}).listen(5000); 


    // HTTP SERVER 
    http.createServer(function (req, response) { 

     var body = 'TEXT FROM HTTP'; 
     response.writeHead(200, { 
     'Content-Length': body.length, 
     'Content-Type': 'text/plain' }); 

     response.write(body); 
     response.end(); 

    }).listen(2525); 

而結果:

方法1:0.385564錯誤:26 方法2:0.062286錯誤:0 方法3:0.255954錯誤:0

我認爲HTTP方法的錯誤是因爲由相同的客戶端(PHP)SIMULT連接。 正如所預測的那樣,快速打開+循環+關閉,最慢的是HTTP

PD:底片爲..?