welsonjs/lib/websocket.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-06-19 21:42:03 +00:00
////////////////////////////////////////////////////////////////////////
// Websocket API
////////////////////////////////////////////////////////////////////////
// references:
// https://stackoverflow.com/questions/52783655/use-curl-with-chrome-remote-debugging
// https://github.com/vi/websocat
var SHELL = require("lib/shell");
var SYS = require("lib/system");
2021-06-19 21:47:04 +00:00
var FILE = require("lib/file");
2021-06-19 21:42:03 +00:00
var WebsocketObject = function() {
2022-01-18 06:38:00 +00:00
this.binPath = "bin\\websocat_win64";
2022-02-24 08:28:33 +00:00
this.timeout = 0;
2021-06-19 21:42:03 +00:00
2021-08-11 07:29:20 +00:00
this.setBinPath = function(path) {
if (typeof(path) !== "undefined") {
this.binPath = path;
} else {
var arch = SYS.getArch();
2022-01-18 06:38:00 +00:00
if(arch.indexOf("64") > -1) {
this.binPath = "bin\\websocat_win64";
2021-08-11 07:29:20 +00:00
} else {
2022-01-18 06:38:00 +00:00
this.binPath = "bin\\x86\\websocat_win32";
2021-08-11 07:29:20 +00:00
}
}
};
2022-01-18 06:38:00 +00:00
2022-02-24 08:28:33 +00:00
this.setTimeout = function(timeout) {
this.timeout = timeout;
};
2021-08-11 07:29:20 +00:00
this.send = function(uri, msg) {
2021-08-11 07:35:37 +00:00
var seed = parseInt(Math.random() * 10000);
2021-08-11 07:29:20 +00:00
var FN = "tmp\\stdin_" + seed + ".txt";
2021-06-24 19:10:53 +00:00
2021-08-11 07:29:20 +00:00
try {
FILE.writeFile(FN, msg + "\n", FILE.CdoCharset.CdoUTF_8);
2021-08-11 07:29:20 +00:00
console.log(msg);
sleep(1);
2022-02-24 08:28:33 +00:00
var cmd = [this.binPath, "-n1", "-t"];
if (this.timeout > 0) {
cmd.push("--ping-timeout");
cmd.push(this.timeout);
}
cmd.push(uri);
cmd.push("<");
cmd.push(FN);
2021-06-19 21:47:04 +00:00
2022-02-24 08:28:33 +00:00
return SHELL.exec(cmd);
2021-08-11 07:29:20 +00:00
} catch (e) {
console.error("WebsocketObject.send() -> " + e.message);
}
2021-06-19 21:42:03 +00:00
2021-08-11 07:29:20 +00:00
if (FILE.fileExists(FN)) FILE.deleteFile(FN);
};
2021-06-19 21:42:03 +00:00
2021-08-11 07:29:20 +00:00
this.create = function() {
this.setBinPath();
};
this.create();
2021-06-19 21:42:03 +00:00
};
exports.VERSIONINFO = "Websocket Lib (websocket.js) version 0.2.1";
2021-06-19 21:42:03 +00:00
exports.global = global;
exports.require = global.require;
exports.create = function() {
2021-08-11 07:29:20 +00:00
return new WebsocketObject();
2021-06-19 21:42:03 +00:00
};