Skip to content

Commit 23598b0

Browse files
websockets work now
1 parent f41a0c1 commit 23598b0

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

draft_code/modwebsocket_test.py

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
import ubinascii
44
from websocket import websocket
55
import time
6+
import select
7+
import gc
8+
9+
# Log memory usage
10+
def log_memory():
11+
gc.collect()
12+
print('Free memory:', gc.mem_free())
613

714
# Connect to Wi-Fi (disabled as per your code)
815
if False:
@@ -14,14 +21,13 @@
1421
print('Connected:', wlan.ifconfig())
1522

1623
# Resolve hostname
17-
# Option 1: echo.websocket.events (unreliable)
18-
host = 'echo.websocket.events'
24+
# Option 1: ws.postman-echo.com (recommended for reliable echo)
25+
host = 'ws.postman-echo.com'
1926
port = 443
20-
handshake_path = '/'
21-
# Option 2: ws.postman-echo.com (recommended for testing)
22-
# host = 'ws.postman-echo.com'
23-
# port = 443
24-
# handshake_path = '/raw'
27+
handshake_path = '/raw'
28+
# Option 2: echo.websocket.events (unreliable)
29+
# host = 'echo.websocket.events'
30+
# handshake_path = '/'
2531

2632
try:
2733
addr_info = socket.getaddrinfo(host, port)[0][-1]
@@ -44,10 +50,12 @@
4450
try:
4551
ssl_sock = ssl.wrap_socket(sock, server_hostname=host)
4652
print('SSL connection established')
53+
print('SSL cipher:', ssl_sock.cipher())
4754
except Exception as e:
4855
print('SSL wrap failed:', e)
4956
sock.close()
5057
raise
58+
log_memory()
5159

5260
# Set socket to non-blocking
5361
ssl_sock.setblocking(False)
@@ -74,15 +82,29 @@
7482
print('Failed to send handshake:', e)
7583
ssl_sock.close()
7684
raise
85+
log_memory()
7786

78-
# Read HTTP response until \r\n\r\n (minimize reading)
87+
# Read HTTP response until \r\n\r\n
7988
response_bytes = bytearray()
8089
max_read = 1024
81-
read_timeout = 2
90+
read_timeout = 5 # Increased to 5s for server response
8291
start_time = time.time()
92+
poller = select.poll()
93+
poller.register(ssl_sock, select.POLLIN)
8394
while len(response_bytes) < max_read:
95+
events = poller.poll(100) # Wait 100ms
96+
print('Poll events:', events)
97+
if not events:
98+
if time.time() - start_time > read_timeout:
99+
print('Read timeout reached')
100+
break
101+
continue
84102
try:
85103
chunk = ssl_sock.read(128)
104+
print('Read attempt, chunk:', chunk)
105+
if chunk is None:
106+
print('Read returned None, continuing to poll')
107+
continue
86108
if not chunk:
87109
print('No more data received (EOF)')
88110
break
@@ -91,7 +113,6 @@
91113
print('Total bytes received:', len(response_bytes))
92114
if b'\r\n\r\n' in response_bytes:
93115
print('End of HTTP headers detected')
94-
# Decode and verify HTTP response immediately
95116
http_end = response_bytes.find(b'\r\n\r\n') + 4
96117
if http_end < 4:
97118
ssl_sock.close()
@@ -110,15 +131,12 @@
110131
print('Handshake response:', response)
111132
ssl_sock.close()
112133
raise Exception('Handshake failed')
113-
# Stop reading to leave WebSocket frame in socket buffer
114134
print('Stopping read to preserve WebSocket frame')
115135
break
116136
except Exception as e:
117137
print('Error reading chunk:', e)
118138
break
119-
if time.time() - start_time > read_timeout:
120-
print('Read timeout reached')
121-
break
139+
log_memory()
122140

123141
# Create WebSocket object
124142
try:
@@ -128,8 +146,9 @@
128146
print('Failed to create WebSocket object:', e)
129147
ssl_sock.close()
130148
raise
149+
log_memory()
131150

132-
# Send and receive data with retries
151+
# Send and receive data with polling
133152
try:
134153
bytes_written = ws.write('Hello, Secure WebSocket!')
135154
print('Sent message, bytes written:', bytes_written)
@@ -138,23 +157,29 @@
138157
ws.close()
139158
raise
140159

141-
# Try reading multiple times to catch initial message or echo
142-
max_attempts = 5
143-
read_timeout = 1 # Short timeout per read
160+
# Poll for data with retries
161+
max_attempts = 10 # Increased retries
162+
poll_timeout = 100 # 100ms per poll
163+
start_time = time.time()
144164
for attempt in range(max_attempts):
165+
events = poller.poll(poll_timeout)
166+
print('Read poll attempt', attempt + 1, 'events:', events)
167+
if not events:
168+
print('Read attempt', attempt + 1, 'no data available')
169+
if time.time() - start_time > 2: # 2s total timeout
170+
print('Read timeout reached')
171+
break
172+
continue
145173
try:
146174
data = ws.read(1024)
147175
print('Read attempt', attempt + 1, 'received:', data)
148176
if data:
149177
break
150-
time.sleep(0.5) # Wait briefly before retrying
151178
except Exception as e:
152179
print('Read attempt', attempt + 1, 'error:', e)
153-
time.sleep(0.5)
154-
if time.time() - start_time > read_timeout:
155-
print('Read timeout reached')
156-
break
180+
time.sleep(0.1)
157181

158182
# Close connection
159183
ws.close()
160184
print('Connection closed')
185+
log_memory()

0 commit comments

Comments
 (0)