var child_process = require('child_process'); var ws = require("nodejs-websocket"); console.log("initialing conn...") var server = ws.createServer(function(conn){ conn.on("text", function (str) { var arr=str.split(" "); var comm=arr.shift(); console.log("received:"+str); aa(comm,arr,conn); }); conn.on("error", function (code, reason) { console.log("异常关闭") }); }).listen(8002) function aa(comm,arr,conn){ var spawnObj = child_process.spawn(comm,arr, {encoding: 'utf-8'}); spawnObj.stdout.on('data', function(chunk) { conn.sendText(chunk.toString(
)); }); spawnObj.stderr.on('data',(data)=>{ console.log('error:${data}'); }); spawnObj.on('error',(err)=>{ console.log('subProcess Failed'+err); }); } console.log("WebSocket建立完毕")