Skip to content
Open
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
54 changes: 54 additions & 0 deletions starfyre/js/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,57 @@ def get_signal(*args, **kwargs):
return store.get(id, initial_state)

return [use_signal, set_signal, get_signal]


import json
import random

# Assuming connection is already established
connection = js.WebSocket.new("ws://localhost:8080/web_socket")


def on_message(event):
print("Message received:", event.data)
try:
data = json.loads(event.data)
except SyntaxError:
print("Invalid JSON")
return
# Process the data, update local state, notify observers, etc.


connection.onmessage = on_message


def server_state():
id = random.randint(0, 100000)

def use_server_signal(element=None):
nonlocal id
if element:
# Send a request to register observer
message = json.dumps({"action": "register", "id": id, "element": element})
connection.send(message)

# Request current state for this id
connection.send(json.dumps({"action": "get_state", "id": id}))

# Returning initial state or undefined, as actual state will be set on server response
return None

def set_server_signal(state):
nonlocal id
# Send the updated state to server
connection.send(json.dumps({"action": "set_state", "id": id, "state": state}))

def get_server_signal():
nonlocal id
# Request current state for this id
connection.send(json.dumps({"action": "get_state", "id": id}))
# Returning initial state or undefined, as actual state will be set on server response
return None

return [use_server_signal, set_server_signal, get_server_signal]


# Handle incoming WebSocket messages
3 changes: 3 additions & 0 deletions test_application/pages/__init__.fyre
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ def mocked_request():
# client functions can be async
[use_parent_signal, set_parent_signal, get_parent_signal] = create_signal(1)

[use_server_signal, set_server_signal, get_server_signal] = server_state()

def handle_on_click(e):
signal_value = get_parent_signal()
set_server_signal(1)
set_parent_signal(signal_value+1)
---

Expand Down
25 changes: 25 additions & 0 deletions test_application/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from robyn import Robyn, WebSocket

app = Robyn(__file__)
websocket = WebSocket(app, "/web_socket")


@websocket.on("message")
def connect(ws, msg):
print(msg)
print(ws)
return message


@websocket.on("close")
def close():
return '{"message": "Goodbye"}'


@websocket.on("connect")
def message():
return '{"message": "Hello world, from ws"}'


if __name__ == "__main__":
app.start()