diff --git a/bin/wscat b/bin/wscat old mode 100755 new mode 100644 index 86ef345..75cab43 --- a/bin/wscat +++ b/bin/wscat @@ -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) { @@ -125,6 +126,8 @@ program .option('-c, --connect ', 'connect to a websocket server') .option('-p, --protocol ', 'optional protocol version') .option('-o, --origin ', 'optional origin') + .option('-x, --execute ', 'execute command after connecting') + .option('-w, --wait ', 'wait given seconds after executing command') .option('--host ', 'optional host') .option('-s, --subprotocol ', 'optional subprotocol') .option('-n, --no-check', 'Do not check for unauthorized certificates') @@ -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) { @@ -254,4 +272,4 @@ if (program.listen && program.connect) { } } else { program.help(); -} +} \ No newline at end of file