Skip to content

Commit d70dce8

Browse files
committed
Allow setting websockets to use binary
Signed-off-by: Nijat K <[email protected]>
1 parent fcea900 commit d70dce8

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

cpp/csp/adapters/websocket/WebsocketEndpoint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ class WebsocketSessionNoTLS final: public WebsocketSession<WebsocketSessionNoTLS
232232
self->m_on_fail(ec.message());
233233
return;
234234
}
235+
if( self->m_properties->get<bool>("binary") )
236+
self->m_ws.binary( true );
235237
self->m_on_open();
236238
self->m_ws.async_read(
237239
self->m_buffer,
@@ -339,6 +341,8 @@ class WebsocketSessionTLS final: public WebsocketSession<WebsocketSessionTLS> {
339341
self->m_on_fail(ec.message());
340342
return;
341343
}
344+
if( self->m_properties->get<bool>("binary") )
345+
self->m_ws.binary( true );
342346
self->m_on_open();
343347
self->m_ws.async_read(
344348
self->m_buffer,

csp/adapters/websocket.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ def __init__(
423423
dynamic: bool = False,
424424
connection_request: Optional[ConnectionRequest] = None,
425425
num_threads: int = 1,
426+
binary: bool = False,
426427
):
427428
"""
428429
uri: str
@@ -436,9 +437,11 @@ def __init__(
436437
num_threads: int = 1
437438
Determines number of threads to allocate for running the websocket endpoints.
438439
Defaults to 1 to avoid thread switching
440+
binary: bool = False
441+
Whether to send/receive text or binary data
439442
"""
440443

441-
self._properties = dict(dynamic=dynamic, num_threads=num_threads)
444+
self._properties = dict(dynamic=dynamic, num_threads=num_threads, binary=binary)
442445
# Enumerating for clarity
443446
if connection_request is not None and uri is not None:
444447
raise ValueError("'connection_request' cannot be set along with 'uri'")
@@ -485,6 +488,7 @@ def _get_properties(self, conn_request: ConnectionRequest) -> dict:
485488
on_connect_payload=conn_request.on_connect_payload,
486489
uri=uri,
487490
dynamic=self._dynamic,
491+
binary=self._properties.get("binary", False),
488492
)
489493
return res
490494

csp/tests/adapters/test_websocket.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def on_message(self, msg):
3030
# Carve-out to allow inspecting the headers
3131
if msg == "header1":
3232
msg = self.request.headers.get(msg, "")
33+
elif not isinstance(msg, str) and msg.decode("utf-8") == "header1":
34+
# Need this for bytes
35+
msg = self.request.headers.get("header1", "")
3336
return self.write_message(msg)
3437

3538
@contextmanager
@@ -98,11 +101,12 @@ def g():
98101
msgs = csp.run(g, starttime=datetime.now(pytz.UTC), realtime=True)
99102
assert msgs["recv"][0][1] == "Hello, World!"
100103

101-
def test_headers(self):
104+
@pytest.mark.parametrize("binary", [False, True])
105+
def test_headers(self, binary):
102106
@csp.graph
103107
def g(dynamic: bool):
104108
if dynamic:
105-
ws = WebsocketAdapterManager(dynamic=True)
109+
ws = WebsocketAdapterManager(dynamic=True, binary=binary)
106110
# Connect with header
107111
conn_request1 = csp.const(
108112
[

0 commit comments

Comments
 (0)