Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addexecute #58

Merged
merged 6 commits into from Dec 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 26 additions & 8 deletions bin/wscat 100755 → 100644
Expand Up @@ -67,7 +67,8 @@ Console.prototype.print = function print(type, msg, color) {
if (tty.isatty(1)) {
this.clear();
color = color || Console.Colors.Default;
if (!program.color) color = '';
if (!program.color || program.execute) color = '';
if (program.execute) type = '';
this.stdout.write(color + type + msg + Console.Colors.Default + '\n');
this.prompt();
} else if (type === Console.Types.Incoming) {
Expand Down Expand Up @@ -125,6 +126,8 @@ program
.option('-c, --connect <url>', 'connect to a websocket server')
.option('-p, --protocol <version>', 'optional protocol version')
.option('-o, --origin <origin>', 'optional origin')
.option('-x, --execute <command>', 'execute command after connecting')
.option('-w, --wait <seconds>', 'wait given seconds after executing command')
.option('--host <host>', 'optional host')
.option('-s, --subprotocol <protocol>', 'optional subprotocol')
.option('-n, --no-check', 'Do not check for unauthorized certificates')
Expand Down Expand Up @@ -214,14 +217,29 @@ if (program.listen && program.connect) {
options.headers = headers;
var ws = new WebSocket(connectUrl, options);

if (program.wait) {
var wait = program.wait*1000;
} else {
var wait = 2000;
}

ws.on('open', function open() {
wsConsole.print(Console.Types.Control, 'connected (press CTRL+C to quit)', Console.Colors.Green);
wsConsole.on('line', function line(data) {
ws.send(data);
wsConsole.prompt();
});
if (program.execute) {
ws.send(program.execute);
setTimeout(function () {
ws.close();
}, wait)
} else {
wsConsole.print(Console.Types.Control, 'connected (press CTRL+C to quit)', Console.Colors.Green);
wsConsole.on('line', function line(data) {
ws.send(data);
wsConsole.prompt();
});
}
}).on('close', function close() {
wsConsole.print(Console.Types.Control, 'disconnected', Console.Colors.Green);
if (!program.execute) {
wsConsole.print(Console.Types.Control, 'disconnected', Console.Colors.Green);
}
wsConsole.clear();
process.exit();
}).on('error', function error(code, description) {
Expand Down Expand Up @@ -254,4 +272,4 @@ if (program.listen && program.connect) {
}
} else {
program.help();
}
}