2012-12-04 101 views
5

我使用socket.io創建了一個快速應用程序。現在我想要一個瀏覽器終端與我在節點中創建的repl連接。以下是我想出了將Socket.io流連接到節點repl流

var io = io.listen(server); 
var sessionSockets = new SessionSockets(io, sessionStore, cookieParser); 

sessionSockets.on('connection', function (err, socket, session) { 
    var stream = createStream(socket); 
    var replInstance = repl.start({ 
    prompt: "> ", 
    input: stream, 
    output: stream 
    }); 
    repls[socket.id] = replInstance; 
}); 

var createStream = function(socket) { 
    var stream = new Stream(); 
    stream.readable = true; 
    stream.resume = function() {}; 

    stream.write = function(data) { 
    socket.emit('stdout', data); 
    }; 

    socket.on('stdin', function(data) { 
    // emit data to repl stream - DOES NOT WORK 
    stream.emit('data', data); 
    }); 

    return stream; 
}; 

它所做的已經是推動控制檯輸出到socket.io連接,並將其顯示在前臺。它沒有在repl中輸入來自前端的數據。

想法?

回答

0

我只是想你的代碼,我想你可能需要在你的流線突破:發射REPL認識到它:

// emit data to repl stream - DOES NOT WORK 
stream.emit('data', data+'\n');