Skip to content

Commit 1372acd

Browse files
authored
Merge pull request #58 from gitforwebwork/addexecute
Add execute
2 parents 14f35d2 + 6c9054a commit 1372acd

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

bin/wscat

100755100644
Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ Console.prototype.print = function print(type, msg, color) {
6767
if (tty.isatty(1)) {
6868
this.clear();
6969
color = color || Console.Colors.Default;
70-
if (!program.color) color = '';
70+
if (!program.color || program.execute) color = '';
71+
if (program.execute) type = '';
7172
this.stdout.write(color + type + msg + Console.Colors.Default + '\n');
7273
this.prompt();
7374
} else if (type === Console.Types.Incoming) {
@@ -125,6 +126,8 @@ program
125126
.option('-c, --connect <url>', 'connect to a websocket server')
126127
.option('-p, --protocol <version>', 'optional protocol version')
127128
.option('-o, --origin <origin>', 'optional origin')
129+
.option('-x, --execute <command>', 'execute command after connecting')
130+
.option('-w, --wait <seconds>', 'wait given seconds after executing command')
128131
.option('--host <host>', 'optional host')
129132
.option('-s, --subprotocol <protocol>', 'optional subprotocol')
130133
.option('-n, --no-check', 'Do not check for unauthorized certificates')
@@ -214,14 +217,29 @@ if (program.listen && program.connect) {
214217
options.headers = headers;
215218
var ws = new WebSocket(connectUrl, options);
216219

220+
if (program.wait) {
221+
var wait = program.wait*1000;
222+
} else {
223+
var wait = 2000;
224+
}
225+
217226
ws.on('open', function open() {
218-
wsConsole.print(Console.Types.Control, 'connected (press CTRL+C to quit)', Console.Colors.Green);
219-
wsConsole.on('line', function line(data) {
220-
ws.send(data);
221-
wsConsole.prompt();
222-
});
227+
if (program.execute) {
228+
ws.send(program.execute);
229+
setTimeout(function () {
230+
ws.close();
231+
}, wait)
232+
} else {
233+
wsConsole.print(Console.Types.Control, 'connected (press CTRL+C to quit)', Console.Colors.Green);
234+
wsConsole.on('line', function line(data) {
235+
ws.send(data);
236+
wsConsole.prompt();
237+
});
238+
}
223239
}).on('close', function close() {
224-
wsConsole.print(Console.Types.Control, 'disconnected', Console.Colors.Green);
240+
if (!program.execute) {
241+
wsConsole.print(Console.Types.Control, 'disconnected', Console.Colors.Green);
242+
}
225243
wsConsole.clear();
226244
process.exit();
227245
}).on('error', function error(code, description) {
@@ -254,4 +272,4 @@ if (program.listen && program.connect) {
254272
}
255273
} else {
256274
program.help();
257-
}
275+
}

0 commit comments

Comments
 (0)