我正在创建一个节点服务器,根据请求生成一个java进程,我想动态地将java输出传递给客户端,例如在浏览器上显示实时日志记录。
为此,我想使用' socket.emit'在' java.stdout / err.on'中,使用以下代码结构,但是' socket'在java块中未定义:
function javaSTI(userid, email, targetUrl, parserclass, selectedTableIndex, socket, fn){
var spawn = require('child_process').spawn;
var java = spawn('java', ['-cp', STI_LIB, STI_MAIN_CLASS, userid, email, targetUrl, STI_OUTPUT_FOLDER, parserclass, selectedTableIndex, STI_PROPERTY]);
var error='';
//### this line generates error1
socket.emit('java_sti',{msg:'ok'});
java.stdout.on('data', function(data){
var txt=replaceAll('\n',data.toString(),'<br/>')+'<br/>';
//### this line generates error2 (if the above line causing error is commented)
socket.emit('sti_info', {msg:txt});
});
java.stderr.on('data', function(data){
var txt=replaceAll('\n',data.toString(),'<br/>')+'<br/>';
socket.emit('sti_err', {msg:txt});
});
var result;
java.on('close', function(code){
var returnWebpage=subfolder+'/'+STI_ERROR_PAGE;
console.log('java process exited with code %s', code);
socket.emit('sti_complete', {msg:'file to display'});
fn(userid);
});
}
io.of('/').on('connection', function(socket){
socket.on('java_sti', function(data){
//### this line is fine
socket.emit('java_sti',{msg:'ok'});
//spawn the sti java process, and pass the socket object ('this')
javaSTI(localUserId,data.url, data.tableparserClass, data.tableIndexes, this, onSTIComplete);
});
}
ERROR1 :
Missing error handler on `socket`.
socket.js:404 TypeError: undefined is not a function
at javaSTI (startserver.js:126:11)
at Socket.<anonymous> (startserver.js:197:4)
at Socket.emit (events.js:107:17)
at Socket.onevent (/node_modules/socket.io/lib/socket.js:335:8)
at Socket.onpacket (/node_modules/socket.io/lib/socket.js:295:12)
at Client.ondecoded (/node_modules/socket.io/lib/client.js:193:14)
at Decoder.Emitter.emit (/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
at Decoder.add (/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)
at Client.ondata (/node_modules/socket.io/lib/client.js:175:18)
at Socket.emit (events.js:107:17)
erro2
startserver.js:130
socket.emit('sti_info', {msg:txt});
^
TypeError: undefined is not a function
at Socket.<anonymous> (startserver.js:130:14)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at Pipe.onread (net.js:538:20)
无论如何我可以解决它吗?
由于
编辑:刚刚按要求添加了该功能的完整代码
答案 0 :(得分:0)
也许问题在这里?
表示子进程stdout的可读流。如果 如果
stdio[1]
设置为'pipe'
以外的任何其他内容,则会生成该子项, 然后这将是未定义
答案 1 :(得分:0)
你的函数需要7个参数:
function javaSTI(userid, email, targetUrl, parserclass, selectedTableIndex, socket, fn)
1 2 3 4 5 6 7
你打电话时只传递6个参数:
javaSTI(localUserId,data.url, data.tableparserClass, data.tableIndexes, this, onSTIComplete)
1 2 3 4 5 6