Skip to content

Commit 3fac8d4

Browse files
author
Alan Christie
committed
chore: Better ws-listener (messages disassembled)
1 parent 832dd82 commit 3fac8d4

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

ws_listener.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Usage: ws_listener.py <location>
55
"""
66
import argparse
7+
import json
78

89
from simple_websocket import Client, ConnectionClosed
910

@@ -22,7 +23,34 @@ def main(c_args: argparse.Namespace):
2223
try:
2324
while True:
2425
data = ws.receive()
25-
print(data)
26+
msg_type = "(unknown)"
27+
msg = "(unknown)"
28+
ordinal = "(unknown)"
29+
timestamp = "(unknown)"
30+
if data[0] == "{":
31+
# A JSON message
32+
data_map = json.loads(data)
33+
msg_type = data_map["message_type"]
34+
msg = data_map["message_body"]
35+
ordinal = data_map["ess_ordinal"]
36+
timestamp = data_map["ess_timestamp"]
37+
else:
38+
# A protocol buffer message
39+
sections = data.split("|")
40+
print(data)
41+
msg_type = sections[0]
42+
msg = sections[1]
43+
if len(sections) > 2:
44+
ordinal = sections[2].split()[1]
45+
timestamp = sections[3].split()[1]
46+
else:
47+
ordinal = "(not present)"
48+
timestamp = "(not present)"
49+
print("---------")
50+
print(f" ORDINAL: {ordinal}")
51+
print(f"TIMESTAMP: {timestamp}")
52+
print(f" TYPE: {msg_type}")
53+
print(f" BODY: {msg}")
2654
except (KeyboardInterrupt, EOFError, ConnectionClosed):
2755
ws.close()
2856

0 commit comments

Comments
 (0)