I'm trying to create a bridge between my application created in PHP and Node.JS.
Node.JS creates socket and listening to it, my code:
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 );
});
And my PHP code just connecting with exists socket and send some data:
$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.
}
Everything should working, but in Node.JS console I see response:
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
And in PHP I see just first message, hello
. Why and how can I fix that?