Created
July 10, 2020 06:42
-
-
Save anjannath/30389112f4d75f5e1b89f248264a5c64 to your computer and use it in GitHub Desktop.
Javascript to talk to crc daemon
This file contains hidden or 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
const net = require('net'); | |
const homedir = require('os').homedir(); | |
const path = require('path'); | |
const SOCKET_PATH = path.join(homedir, ".crc", "crc.sock"); | |
function sendCommand(command, args) { | |
if (command === "") { | |
console.log("Command is required!!") | |
return | |
} | |
sock = net.createConnection(SOCKET_PATH, () => { | |
sock.on('error', (e) => { | |
console.error(e) | |
}) | |
cmd = { | |
"command": command, | |
"args": args | |
} | |
console.log(JSON.stringify(cmd)) | |
sock.write(JSON.stringify(cmd)); | |
// receive json response | |
sock.on('data', (d) => { | |
console.log(d.toString()) | |
}) | |
sock.on('error', (e) => { | |
console.log(e) | |
}) | |
}) | |
} | |
sendCommand("version") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment