websockets / wscat

WebSocket cat
MIT License
2.25k stars 223 forks source link

Added support for multiline JSON parsing #140

Closed DeiNile closed 1 month ago

DeiNile commented 2 years ago

It is pretty common to send JSON over websockets, and it can be rather limiting to be forced to "minify" the JSON string, especially for local testing. The new '-j --json' flag is supposed to alleviate that. The primary way this is alleviated is by allowing for multiline inputs. Secondarily, by leveraging the built in JSON.parse() method we can check to see if the inputs produce a valid JSON string, and determine that the multiline input is terminated. Finally, as inspired by the Scala REPL, two consecutive empty lines resets the input.

Here are some samples of what this looks like while it is running:

$ wscat -j -c ws://websocket-echo.com
Connected (press CTRL+C to quit)
> {}
< {}
> []
< []
> {
|   "foo": "bar",
|   "hello": "world"
| }
< {
  "foo": "bar",
  "hello": "world"
}
> 42
< 42
> "hello world"
< "hello world"
> [
|   1,
|   2
|   ,3]
< [
  1,
  2
  ,3]
lpinca commented 1 month ago

I'm going to revert this. For anyone reading and interested in having this, feel free to propose a solution that addresses https://github.com/websockets/wscat/pull/140#discussion_r1791752256 and https://github.com/websockets/wscat/pull/140#discussion_r1791757317.