web socket server code, 調用 shell exec child_process
var child_process = require(‘child_process‘);
var ws = require("nodejs-websocket");
console.log("開始建立連接...")
var server = ws.createServer(function(conn){
conn.on("text", function (str) {
var arr=str.split(" ");
var comm=arr.shift();
console.log("收到的信息為:"+str)
console.log("******"+comm+"*******");
var command = comm;
var argF=arr;
var spawnObj = child_process.spawn(comm,arr, {encoding: ‘utf-8‘});
spawnObj.stdout.on(‘data‘, function(chunk) {
console.log("pppp"+chunk.toString());
conn.sendText(chunk.toLocaleString());
});
});
conn.on("close", function (code, reason) {
console.log("關閉連接")
});
conn.on("error", function (code, reason) {
console.log("異常關閉")
});
}).listen(8002)
console.log("WebSocket建立完畢")
web socket server code, 調用 shell exec child_process