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

Pretty print JSON #74

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
17 changes: 15 additions & 2 deletions bin/wscat
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ program
"If you don't provide a value, it will be prompted for"
)
.option('--no-color', 'run without color')
.option('--pretty', 'attempt to pretty print responses using JSON.stringify.')
.option(
'--slash',
'enable slash commands for control frames (/ping, /pong, /close ' +
Expand Down Expand Up @@ -334,9 +335,21 @@ if (program.listen) {
wsConsole.print(Console.Types.Error, err.message, Console.Colors.Yellow);
process.exit(-1);
});

ws.on('message', (data) => {
wsConsole.print(Console.Types.Incoming, data, Console.Colors.Blue);
let s = data;
if (program.pretty) {
try {
s = JSON.stringify(JSON.parse(data), null, 4);
} catch(e) {
s = data;
}
}
wsConsole.print(Console.Types.Incoming, s, Console.Colors.Blue);
}).on('ping', function ping() {
wsConsole.print(Console.Types.Incoming, 'Received ping', Console.Colors.Blue);
}).on('pong', function pong() {
wsConsole.print(Console.Types.Incoming, 'Received pong', Console.Colors.Blue);
});

ws.on('ping', () => {
Expand Down