diff --git a/native-messaging/app/ping_pong.py b/native-messaging/app/ping_pong.py index c987f60..e934380 100755 --- a/native-messaging/app/ping_pong.py +++ b/native-messaging/app/ping_pong.py @@ -18,7 +18,12 @@ try: # Encode a message for transmission, # given its content. def encodeMessage(messageContent): - encodedContent = json.dumps(messageContent).encode('utf-8') + # https://docs.python.org/3/library/json.html#basic-usage + # To get the most compact JSON representation, you should specify + # (',', ':') to eliminate whitespace. + # We want the most compact representation because the browser rejects + # messages that exceed 1 MB. + encodedContent = json.dumps(messageContent, separators=(',', ':')).encode('utf-8') encodedLength = struct.pack('@I', len(encodedContent)) return {'length': encodedLength, 'content': encodedContent} @@ -46,7 +51,12 @@ except AttributeError: # Encode a message for transmission, # given its content. def encodeMessage(messageContent): - encodedContent = json.dumps(messageContent) + # https://docs.python.org/3/library/json.html#basic-usage + # To get the most compact JSON representation, you should specify + # (',', ':') to eliminate whitespace. + # We want the most compact representation because the browser rejects + # messages that exceed 1 MB. + encodedContent = json.dumps(messageContent, separators=(',', ':')) encodedLength = struct.pack('@I', len(encodedContent)) return {'length': encodedLength, 'content': encodedContent}