I am working on a live terminal in the browser over socket.io. And I encountered a problem.
My code:
child = require('child_process').spawn('cli opening command'),
//simple output to the client
child.stdout.on('data', function(data) {
io.emit('consoleOut', data);
});
//socket
io.on('connection', (socket) => {
socket.on('consoleInput', (msg) => {
//writing the message to the stdin stream
child.stdin.write(msg + '\n');
})
});
The Problem:
With this setup, the output is always "No such command '[user input]'..." only on the first input after the CLI was started it is working fine. And when I look at the output, I see that the first letter between the two quotes is an ASCII decimal 4 that equals to an end of transmission character.
Example (if I look at the string in the browser):
No such command '\u0004set verbose 5'
And the weird thing is when I write to the stream and hardcode it, the commands are perfectly fine and get executed like they should. But if then a user inputs his command, again every command is not working, even the first one.
child.stdin.write('set verbose 5\n');
child.stdin.write('set verbose 3\n');
I can verify that it is not the user input because when I change the socket on the consoleInput function into:
child.stdin.write('test' + msg + '\n');
I still get the same output (if for example the user inputs set verbose 5):
No such command '\u0004testset verbose 5'
(of course the command wouldn't work in the first place, but it's to show for the EOT chat)
My guess:
I get the EOT char only when I write to the buffer, and my string includes at least one \n. Then everything until the last \n gets sliced out of the buffer, and there is then a EOT char left behind. And It makes sense if this is true that I can input one command at the start that is working fine because there the whole buffer is empty and no EOT char is there.
Thanks, Julian ;)