annotate patch_cyclone.py @ 19:8b376bffa396

release 0.18.0
author drewp@bigasterisk.com
date Wed, 24 Nov 2021 20:06:20 -0800
parents 1b6718a54c00
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
1
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
2
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
3 def patch_sse():
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
4 from cyclone import escape
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
5 import cyclone.sse
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
6 def new_sendEvent(self, message, event=None, eid=None, retry=None):
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
7 if isinstance(message, dict):
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
8 message = escape.json_encode(message)
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
9 if isinstance(message, str):
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
10 message = message.encode("utf-8")
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
11 assert isinstance(message, bytes)
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
12 if eid:
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
13 self.transport.write(b"id: " + eid.encode("utf-8") + b"\n")
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
14 if event:
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
15 self.transport.write(b"event: " + event + b"\n")
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
16 if retry:
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
17 self.transport.write(b"retry: " + retry.encode("utf-8") + b"\n")
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
18 self.transport.write(b"data: " + message + b"\n\n")
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
19
1b6718a54c00 add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff changeset
20 cyclone.sse.SSEHandler.sendEvent = new_sendEvent