-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebsocket.qs
More file actions
40 lines (30 loc) · 1007 Bytes
/
Copy pathwebsocket.qs
File metadata and controls
40 lines (30 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# WebSocket echo demo
# Demonstrates the WebSocket client API
from "../stdlib/websocket.qs" import connect, send, recv, close, try_connect, set_timeout, is_open
# Try to connect to an echo server
print("Connecting to echo server...")
ws = try_connect("ws://echo.websocket.org")
if ws == null {
print("Could not connect to echo server")
print("This example requires a reachable WebSocket echo server")
} else {
print("Connected!")
# Set a 3-second timeout for receives
set_timeout(ws, 3000)
# Send a text message
send(ws, "Hello from QuantoScript!")
print("Sent: Hello from QuantoScript!")
# Receive the echo response
msg = recv(ws)
print("Received:", msg)
# Send JSON
send(ws, '{"action": "echo", "data": [1, 2, 3]}')
msg = recv(ws)
print("JSON response:", msg)
# Check connection state
print("Connection open:", is_open(ws))
# Close gracefully
close(ws)
print("Connection closed")
}
print("WebSocket demo complete")