Skip to content

Commit 1ff52dd

Browse files
committed
update
1 parent 4c8261e commit 1ff52dd

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

starfyre/js/store.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,23 @@ def get_signal(*args, **kwargs):
140140
import random
141141

142142
# Assuming connection is already established
143-
connection = js.WebSocket("ws://localhost:8765")
143+
connection = js.WebSocket.new("ws://localhost:8080/web_socket")
144+
145+
def on_message(event):
146+
print("Message received:", event.data)
147+
try:
148+
data = json.loads(event.data)
149+
except SyntaxError:
150+
print("Invalid JSON")
151+
return
152+
# Process the data, update local state, notify observers, etc.
153+
154+
connection.onmessage = on_message
144155

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

159+
148160
def use_server_signal(element=None):
149161
nonlocal id
150162
if element:
@@ -173,8 +185,3 @@ def get_server_signal():
173185
return [use_server_signal, set_server_signal, get_server_signal]
174186

175187
# Handle incoming WebSocket messages
176-
def on_message(event):
177-
data = json.loads(event.data)
178-
# Process the data, update local state, notify observers, etc.
179-
180-
connection.onmessage = on_message

test_application/pages/__init__.fyre

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ def mocked_request():
99
# client functions can be async
1010
[use_parent_signal, set_parent_signal, get_parent_signal] = create_signal(1)
1111

12+
[use_server_signal, set_server_signal, get_server_signal] = server_state()
13+
1214
def handle_on_click(e):
1315
signal_value = get_parent_signal()
16+
set_server_signal(1)
1417
set_parent_signal(signal_value+1)
1518
---
1619

test_application/server.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from robyn import Robyn, jsonify, WebSocket
2+
3+
app = Robyn(__file__)
4+
websocket = WebSocket(app, "/web_socket")
5+
6+
@websocket.on("message")
7+
def connect(ws, msg):
8+
print(msg)
9+
print(ws)
10+
return message
11+
12+
@websocket.on("close")
13+
def close():
14+
return '{"message": "Goodbye"}'
15+
16+
@websocket.on("connect")
17+
def message():
18+
return '{"message": "Hello world, from ws"}'
19+
20+
if __name__ == "__main__":
21+
app.start()

0 commit comments

Comments
 (0)