-
-
Save tadfmac/ec1c0452cad4a22c64a90014c9d50a00 to your computer and use it in GitHub Desktop.
ws->exec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ws -> exec bridge | |
// npm i ws | |
const fs = require("fs"); | |
const https = require('https'); | |
const port = 3055; | |
var exec = require('child_process').exec; | |
const server = https.createServer({ | |
cert: fs.readFileSync('/home/pi/_gc/srv/crt/server.crt'), | |
key: fs.readFileSync('/home/pi/_gc/srv/crt/server.key') | |
}); | |
server.listen(port,()=> { | |
console.log("https server started."); | |
}); | |
var WebSocket = require('ws'); | |
var wss = new WebSocket.Server({server:server}); | |
process.on('unhandledRejection', console.dir); | |
var connections = new Map; | |
wss.on('connection', (ws,req)=> { | |
let id=ws._ultron.id; | |
console.log(id); | |
var conn = {ws:ws, uid:ws._socket._handle.fd}; | |
connections.set(conn.uid,conn); | |
console.log("[new connection]uid:"+conn.uid); | |
conn.ws.on('close', ()=>{ | |
console.log("[connection closed]uid:"+conn.uid); | |
connections.delete(conn.uid); | |
}); | |
conn.ws.on('message', (message)=>{ | |
console.log("[onmessage]:"+message); | |
// var mesobj = JSON.parse(message); | |
if(message == "red"){ | |
exec("./red"); | |
}else if(message == "green"){ | |
exec("./green"); | |
}else{ | |
exec("./blue"); | |
} | |
// broadcastMessage(message,conn); | |
}); | |
conn.ws.on('error', (err)=>{ | |
console.log(err); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment