Skip to content

Remove SSL Enabled parameter to connect and change host to spacetimedb_uri #6

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/quickstart/client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def run_client(spacetime_client):
asyncio.run(
spacetime_client.run(
local_config.get_string("auth_token"),
"localhost:3000",
"chat",
False,
"http://localhost:3000",
"chatqs",
on_connect,
["SELECT * FROM User", "SELECT * FROM Message"],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from spacetimedb_sdk.spacetimedb_client import Identity


reducer_name = "send_message"

def send_message(text: str):
text = text
SpacetimeDBClient.instance._reducer_call("send_message", text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from spacetimedb_sdk.spacetimedb_client import Identity


reducer_name = "set_name"

def set_name(name: str):
name = name
SpacetimeDBClient.instance._reducer_call("set_name", name)
Expand Down
40 changes: 27 additions & 13 deletions src/spacetimedb_sdk/spacetime_websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@


class WebSocketClient:
def __init__(self, protocol, on_connect=None, on_close=None, on_error=None, on_message=None):
def __init__(
self, protocol, on_connect=None, on_close=None, on_error=None, on_message=None
):
self._on_connect = on_connect
self._on_close = on_close
self._on_error = on_error
Expand All @@ -14,15 +16,25 @@ def __init__(self, protocol, on_connect=None, on_close=None, on_error=None, on_m
self.protocol = protocol
self.ws = None
self.message_thread = None
self.host = None
self.spacetimedb_uri = None
self.name_or_address = None
self.is_connected = False

def connect(self, auth, host, name_or_address, ssl_enabled):
protocol = "wss" if ssl_enabled else "ws"
url = f"{protocol}://{host}/database/subscribe/{name_or_address}"
def connect(self, auth, spacetimedb_uri, name_or_address):
if spacetimedb_uri[-1] == "/":
spacetimedb_uri = spacetimedb_uri[:-1]

self.host = host
if spacetimedb_uri[:5] == "https":
spacetimedb_uri = "wss" + spacetimedb_uri[5:]
elif spacetimedb_uri[:4] == "http":
spacetimedb_uri = "ws" + spacetimedb_uri[4:]

if spacetimedb_uri[:5] != "ws://" and spacetimedb_uri[:6] != "wss://":
spacetimedb_uri = "ws://" + spacetimedb_uri

url = f"{spacetimedb_uri}/database/subscribe/{name_or_address}"

self.spacetimedb_uri = spacetimedb_uri
self.name_or_address = name_or_address

ws_header = None
Expand All @@ -35,13 +47,15 @@ def connect(self, auth, host, name_or_address, ssl_enabled):
else:
headers = None

self.ws = websocket.WebSocketApp(url,
on_open=self.on_open,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close,
header=headers,
subprotocols=[self.protocol])
self.ws = websocket.WebSocketApp(
url,
on_open=self.on_open,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close,
header=headers,
subprotocols=[self.protocol],
)

self.message_thread = threading.Thread(target=self.ws.run_forever)
self.message_thread.start()
Expand Down
18 changes: 10 additions & 8 deletions src/spacetimedb_sdk/spacetimedb_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ def force_close(self):
async def run(
self,
auth_token,
host,
spacetimedb_uri,
address_or_name,
ssl_enabled,
on_connect,
subscription_queries=[],
):
Expand All @@ -144,7 +143,7 @@ async def run(

Args:
auth_token : authentication token to use when connecting to the server
host : host name or IP address of the server
spacetimedb_uri : URI of the SpacetimeDB server (ex: https://testnet.spacetimedb.com)
address_or_name : address or name of the module to connect to
ssl_enabled : True to use SSL, False to not use SSL
on_connect : function to call when the client connects to the server
Expand All @@ -155,7 +154,7 @@ async def run(
self._on_async_loop_start()

identity_result = await self.connect(
auth_token, host, address_or_name, ssl_enabled, subscription_queries
auth_token, spacetimedb_uri, address_or_name, subscription_queries
)

if on_connect is not None:
Expand Down Expand Up @@ -185,7 +184,11 @@ def on_event(event):
await self.close()

async def connect(
self, auth_token, host, address_or_name, ssl_enabled, subscription_queries=[]
self,
auth_token,
spacetimedb_uri,
address_or_name,
subscription_queries=[],
):
"""
Connect to the server.
Expand All @@ -194,7 +197,7 @@ async def connect(

Args:
auth_token : authentication token to use when connecting to the server
host : host name or IP address of the server
spacetimedb_uri : URI of the SpacetimeDB server (ex: https://testnet.spacetimedb.com)
address_or_name : address or name of the module to connect to
ssl_enabled : True to use SSL, False to not use SSL
subscription_queries : list of queries to subscribe to
Expand All @@ -219,9 +222,8 @@ def on_identity_received(auth_token, identity):

self.client.connect(
auth_token,
host,
spacetimedb_uri,
address_or_name,
ssl_enabled,
on_connect=None,
on_error=on_error,
on_disconnect=on_disconnect,
Expand Down
10 changes: 4 additions & 6 deletions src/spacetimedb_sdk/spacetimedb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,8 @@ def __init__(self, autogen_package):
def connect(
self,
auth_token,
host,
spacetimedb_uri,
address_or_name,
ssl_enabled,
on_connect,
on_disconnect,
on_identity,
Expand All @@ -238,9 +237,8 @@ def connect(
# print("CONNECTING " + host + " " + address_or_name)
self.wsc.connect(
auth_token,
host,
spacetimedb_uri,
address_or_name,
ssl_enabled,
)

def update(self):
Expand Down Expand Up @@ -592,8 +590,8 @@ def _do_update(self):
decode_func = self.client_cache.reducer_cache[
reducer_event.reducer_name
]
if reducer_event.status == "committed":
args = decode_func(reducer_event.args)

args = decode_func(reducer_event.args)

for reducer_callback in self._reducer_callbacks[
reducer_event.reducer_name
Expand Down