Not receiving stdout from nodejs spawned process

后端 未结 3 1843
青春惊慌失措
青春惊慌失措 2021-02-13 11:08

I\'m trying to have nodejs interact with adventure, an old text based game. The idea is to open adventure as a child process and then play the game by writing

3条回答
  •  遥遥无期
    2021-02-13 11:24

    The data passed is a buffer type, not a string. Therefore, you need a decoder to read that buffer and then do the logging.

    Here's how to do that.

    var StringDecoder = require('string_decoder').StringDecoder;
    var decoder = new StringDecoder('utf8');
    
    child.stdout.on('data', function (data) {
        var message = decoder.write(data);
        console.log(message.trim());
    });
    

提交回复
热议问题