welsonjs/lib/websocket.js

76 lines
2.0 KiB
JavaScript
Raw Normal View History

// Websocket API for WelsonJS framework
// Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs
2024-09-25 07:00:41 +00:00
//
2021-06-19 21:42:03 +00:00
// references:
// - https://stackoverflow.com/questions/52783655/use-curl-with-chrome-remote-debugging
// - https://github.com/vi/websocat
2024-09-25 07:00:41 +00:00
//
2021-06-19 21:42:03 +00:00
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() {
2024-09-25 07:00:41 +00:00
this._interface = null;
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") {
2024-09-25 07:00:41 +00:00
this._interface.setPrefix(path);
2021-08-11 07:29:20 +00:00
} else {
var arch = SYS.getArch();
2022-01-18 06:38:00 +00:00
if(arch.indexOf("64") > -1) {
2024-09-25 07:00:41 +00:00
this._interface.setPrefix("bin\\x64\\websocat.x86_64-pc-windows-gnu.exe");
2021-08-11 07:29:20 +00:00
} else {
2024-09-25 07:00:41 +00:00
this._interface.setPrefix("bin\\x86\\websocat.i686-pc-windows-gnu.exe");
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);
2024-09-25 07:00:41 +00:00
var cmd = ["-n1", "-t"];
2022-02-24 08:28:33 +00:00
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
2024-09-25 07:00:41 +00:00
return this._interface.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() {
2024-09-25 07:00:41 +00:00
this._interface = SHELL.create();
2021-08-11 07:29:20 +00:00
this.setBinPath();
};
this.create();
2021-06-19 21:42:03 +00:00
};
2024-09-25 07:00:41 +00:00
exports.VERSIONINFO = "Websocket Interface (websocket.js) version 0.2.3";
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
};