2014-05-07 90 views
2

我試圖在PHP和Node.JS創建的應用程序之間建立橋樑。通過UNIX套接字連接Node.JS和PHP - EPIPE寫入錯誤

的Node.js創建插座,聽着它,我的代碼:

var net = require('net'), 
    fs = require('fs'); 
var path = '/tmp/echo.sock'; 

fs.unlink(path, function() { 
    var server = net.createServer(function(c) { 

    console.log('server connected'); 

    c.on('close', function() { 
     console.log('server disconnected'); 
    }); 
    c.write('hello\r\n'); 

    c.on('data', function(data) { 
     console.log('Response: "' + data + '"'); 
     c.write('You said "' + data + '"'); 
    }); 

    }); 
    server.listen(path, function(e) { 
    console.log('server bound on %s', path); 
    }); 
}); 

process.on('uncaughtException', function (err) { 
    console.log("UNCAUGHT EXCEPTION "); 
    console.log("[Inside 'uncaughtException' event] " + err.stack || err.message); 
}); 

而只是存在插座,發送一些數據連接我的PHP代碼:

$fp = fsockopen("unix:///tmp/echo.sock", -1, $errno, $errstr); 
if (!$fp) { 
    return "ERROR: $errno - $errstr<br />\n"; 
} else { 
    fwrite($fp, "Hello World <3"); 
    $out = fread($fp, 8192); 
    fclose($fp); 
    return $out; // That code is in function. 
} 

一切都應該工作,但在Node.JS控制檯中,我看到了響應:

server bound on /tmp/echo.sock 
server connected 
Response: "Hello World <3" 
UNCAUGHT EXCEPTION 
[Inside 'uncaughtException' event] Error: write EPIPE 
    at exports._errnoException (util.js:745:11) 
    at Object.afterWrite (net.js:763:14) 
server disconnected 

而在PHP中,我只看到第一條消息hello。爲什麼以及如何解決這個問題?

+1

在PHP你靠近管道的連接從它具有receieved數據後(c.write('你好\ r \ n')在節點js中)。當你的php進程不再監聽時,節點js試圖寫入管道,那麼它會失敗。 –

+0

所以,我只需要使用一次'c.write',對吧? – Siper

+0

或者你可以讓php繼續讀管道,直到它接收到一個特定的命令,然後關閉管道 –

回答

0

我知道問題在哪裏。在PHP中,我在寫入數據後關閉了管道的連接。節點嘗試寫入響應,但它不能,因爲PHP中的連接已經關閉。所以,我剛剛添加檢查是連接仍然存在。當然,下面的這個不是在生產中使用,而是爲了展示它的工作原理。

的Node.js代碼:

var net = require('net'), 
    fs = require('fs'); 
var path = '/tmp/echo.sock', 
    toSend = 1, 
    sended = 0; 

fs.unlink(path, function() { 
    var server = net.createServer(function(c) { 

    console.log('server connected'); 

    c.on('close', function() { 
     console.log('server disconnected'); 
    }); 
    c.write('hello\r\n'); 

    c.on('data', function(data) { 
     console.log('Response: "' + data + '"'); 

     if(data == "IS_ALREADY_CLOSED" & toSend == sended) { 
      c.write('ALREADY_CLOSED');  
      return; 
     } else { 
      c.write('NOT_ALREADY_CLOSED'); 
      return; 
     } 
     c.write('You said "' + data + '"'); 
     sended++; 
    }); 

    }); 
    server.listen(path, function(e) { 
    console.log('server bound on %s', path); 
    }); 
}); 

process.on('uncaughtException', function (err) { 
    console.log("UNCAUGHT EXCEPTION "); 
    console.log("[Inside 'uncaughtException' event] " + err.stack || err.message); 
}); 

而在PHP:

$fp = fsockopen("unix:///tmp/echo.sock", -1, $errno, $errstr); 
if (!$fp) { 
    return "ERROR: $errno - $errstr<br />\n"; 
} else { 
    fwrite($fp, "Hello World <3"); 

    do { 
     $output = fread($fp, 8192); 
     if($output == "ALREADY_CLOSED") break; 
     elseif($output == "NOT_ALREADY_CLOSED") continue; 

     echo $output . "\n"; 
     fwrite($fp, "IS_ALREADY_CLOSED"); 
    } while(true); 

    fclose($fp); 
} 
0

試試這個;

<?php 
$fp = fsockopen("unix:///tmp/echo.sock", -1, $errno, $errstr); 
if (!$fp) { 
    return "ERROR: $errno - $errstr<br />\n"; 
} else { 
    $out = fread($fp, 8192); 
    fwrite($fp, "Hello World <3"); 
    $out2 = fread($fp, 8192); 
    fclose($fp); 
    echo $out; // That code is in function. 
    echo $out2; // That code is in function. 
} 
?> 
相關問題