I've this Ruby server that uses a Unix Socket:
require 'socket'
require 'json'
server = UNIXServer.new('/tmp/ciccio.sock')
loop do
sock = server.accept
loop do
begin
data = sock.recv(1024)
break if data.empty?
# calculate a response
response = {supermega: "very long json" * 10000}
sock.write(response.to_json)
sock.flush
rescue Errno::EPIPE, Errno::ENOTCONN
break
end
end
end
And I've this client in JavaScript that uses node.js net api:
Net = require('net');
var client = Net.connect({ path: '/tmp/ciccio.sock' }, () => {
console.log('write data');
client.write('hello world!');
});
client.on('data', (data) => {
console.log(data.toString());
var json = JSON.parse(data);
// do something with json
});
client.on('end', () => {
console.log('end');
});
client.on('error', (error) => {
console.log(error.toString());
});
The problem is that if the data is big (over about 8193 characters) JSON.parse
fail because data
is chunked. How can I get the whole json as string and then parse it?