Skip to content

Commit 26fce3b

Browse files
more test code
1 parent 4a27a13 commit 26fce3b

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import uasyncio as asyncio
2+
import sys
3+
from contextlib import suppress
4+
import aiohttp
5+
import _thread
6+
7+
# Shared buffer for input lines
8+
input_buffer = []
9+
lock = _thread.allocate_lock() # Thread-safe access to buffer
10+
11+
# Thread function to read input and add to buffer
12+
def read_input_thread():
13+
while True:
14+
line = input()
15+
with lock:
16+
input_buffer.append(line)
17+
if line == "exit":
18+
break
19+
20+
async def start_client(url: str) -> None:
21+
name = input("Please enter your name: ")
22+
23+
# Start the input reading thread
24+
_thread.start_new_thread(read_input_thread, ())
25+
26+
async def dispatch(ws: aiohttp.ClientWebSocketResponse) -> None:
27+
while True:
28+
#msg = await ws.receive()
29+
msg = await ws.__anext__()
30+
31+
if msg.type is aiohttp.WSMsgType.TEXT:
32+
print("Text: ", msg.data.strip())
33+
elif msg.type is aiohttp.WSMsgType.BINARY:
34+
print("Binary: ", msg.data)
35+
elif msg.type is aiohttp.WSMsgType.PING:
36+
await ws.pong()
37+
elif msg.type is aiohttp.WSMsgType.PONG:
38+
print("Pong received")
39+
else:
40+
if msg.type is aiohttp.WSMsgType.CLOSE:
41+
await ws.close()
42+
elif msg.type is aiohttp.WSMsgType.ERROR:
43+
print("Error during receive %s" % ws.exception())
44+
elif msg.type is aiohttp.WSMsgType.CLOSED:
45+
pass
46+
break
47+
48+
async with aiohttp.ClientSession() as session:
49+
async with session.ws_connect(url) as ws:
50+
dispatch_task = asyncio.create_task(dispatch(ws))
51+
52+
# Poll the input buffer instead of to_thread
53+
while True:
54+
line = None
55+
with lock:
56+
if input_buffer: # Check if there's input
57+
line = input_buffer.pop(0) # Get the first line
58+
if line:
59+
await ws.send_str(name + ": " + line)
60+
if line == "exit": # Stop on "exit"
61+
break
62+
await asyncio.sleep_ms(100) # Avoid busy-waiting
63+
64+
dispatch_task.cancel()
65+
with suppress(asyncio.CancelledError):
66+
await dispatch_task
67+
68+
# Run the client
69+
asyncio.run(start_client("wss://echo.websocket.events"))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# it's not super fast but it works!
2+
3+
import websocket
4+
import _thread
5+
import time
6+
7+
def on_message(wsapp, message):
8+
print(f"got message: {message}")
9+
10+
def on_ping(wsapp, message):
11+
print("Got a ping! A pong reply has already been automatically sent.")
12+
13+
def on_pong(wsapp, message):
14+
print("Got a pong! No need to respond")
15+
16+
17+
def on_error(wsapp, message):
18+
print(f"Got error: {message}")
19+
20+
21+
#wsapp = websocket.WebSocketApp("wss://testnet.binance.vision/ws/btcusdt@trade", on_message=on_message, on_ping=on_ping, on_pong=on_pong, on_error=on_error)
22+
23+
wsapp = websocket.WebSocketApp("wss://echo.websocket.events", on_message=on_message, on_ping=on_ping, on_pong=on_pong, on_error=on_error)
24+
25+
def stress_test_thread():
26+
print("before run_forever")
27+
wsapp.run_forever(ping_interval=15, ping_timeout=10, ping_payload="This is an optional ping payload")
28+
print("after run_forever")
29+
30+
_thread.stack_size(16*1024)
31+
_thread.start_new_thread(stress_test_thread, ())
32+
33+
time.sleep(5)
34+
print("sending ok")
35+
wsapp.send_text('ok')
36+
37+
38+
time.sleep(15)
39+
print("sending again")
40+
wsapp.send_text('again')
41+
42+
43+
time.sleep(25)
44+
print("sending more")
45+
wsapp.send_text('more')
46+
47+
wsapp.close()
48+
49+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from websocket import WebSocketApp
2+
3+
def on_message(ws, message):
4+
print(f"Received: {message}")
5+
6+
def on_open(ws):
7+
ws.send_text("Hello, Nostr!")
8+
9+
ws = WebSocketApp(
10+
url="wss://relay.damus.io",
11+
on_open=on_open,
12+
on_message=on_message,
13+
on_error=lambda ws, e: print(f"Error: {e}"),
14+
on_close=lambda ws, code, reason: print("Closed")
15+
)
16+
ws.run_forever(ping_interval=30, ping_timeout=10)

0 commit comments

Comments
 (0)