Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic endpoints to WebsocketAdapter #378

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Allow setting websockets to use binary
Signed-off-by: Nijat K <nijat.khanbabayev@gmail.com>
NeejWeej committed Nov 25, 2024
commit d70dce8fa84d766f5af8469701a6b3c7bd02da83
4 changes: 4 additions & 0 deletions cpp/csp/adapters/websocket/WebsocketEndpoint.h
Original file line number Diff line number Diff line change
@@ -232,6 +232,8 @@ class WebsocketSessionNoTLS final: public WebsocketSession<WebsocketSessionNoTLS
self->m_on_fail(ec.message());
return;
}
if( self->m_properties->get<bool>("binary") )
self->m_ws.binary( true );
self->m_on_open();
self->m_ws.async_read(
self->m_buffer,
@@ -339,6 +341,8 @@ class WebsocketSessionTLS final: public WebsocketSession<WebsocketSessionTLS> {
self->m_on_fail(ec.message());
return;
}
if( self->m_properties->get<bool>("binary") )
self->m_ws.binary( true );
self->m_on_open();
self->m_ws.async_read(
self->m_buffer,
6 changes: 5 additions & 1 deletion csp/adapters/websocket.py
Original file line number Diff line number Diff line change
@@ -423,6 +423,7 @@ def __init__(
dynamic: bool = False,
connection_request: Optional[ConnectionRequest] = None,
num_threads: int = 1,
binary: bool = False,
):
"""
uri: str
@@ -436,9 +437,11 @@ def __init__(
num_threads: int = 1
Determines number of threads to allocate for running the websocket endpoints.
Defaults to 1 to avoid thread switching
binary: bool = False
Whether to send/receive text or binary data
"""

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

8 changes: 6 additions & 2 deletions csp/tests/adapters/test_websocket.py
Original file line number Diff line number Diff line change
@@ -30,6 +30,9 @@ def on_message(self, msg):
# Carve-out to allow inspecting the headers
if msg == "header1":
msg = self.request.headers.get(msg, "")
elif not isinstance(msg, str) and msg.decode("utf-8") == "header1":
# Need this for bytes
msg = self.request.headers.get("header1", "")
return self.write_message(msg)

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

def test_headers(self):
@pytest.mark.parametrize("binary", [False, True])
def test_headers(self, binary):
@csp.graph
def g(dynamic: bool):
if dynamic:
ws = WebsocketAdapterManager(dynamic=True)
ws = WebsocketAdapterManager(dynamic=True, binary=binary)
# Connect with header
conn_request1 = csp.const(
[