Files
wren/example/websocket_server_demo.wren
T
retoor fdd61cc90c feat: add cJSON and SQLite amalgamation source files under deps/
Add the cJSON library (cJSON.c, cJSON.h) and SQLite amalgamation
(sqlite3.c, sqlite3.h, sqlite3ext.h, shell.c) as vendored dependencies
in the deps/ directory for use by the project build system.
2026-01-24 18:35:43 +00:00

45 lines
957 B
JavaScript
Vendored

// retoor <retoor@molodetz.nl>
import "websocket" for WebSocketServer
System.print("=== WebSocket Server Demo ===")
System.print("Listening on ws://0.0.0.0:8080\n")
var server = WebSocketServer.bind("0.0.0.0", 8080)
while (true) {
System.print("Waiting for connection...")
var ws = server.accept()
if (ws == null) {
System.print("Failed to accept connection")
continue
}
System.print("Client connected")
while (ws.isOpen) {
var msg = ws.receive()
if (msg == null) {
System.print("Connection lost")
break
}
if (msg.isClose) {
System.print("Client sent close frame (code: %(msg.closeCode))")
break
}
if (msg.isText) {
System.print("Received text: %(msg.text)")
ws.send("Echo: %(msg.text)")
} else if (msg.isBinary) {
System.print("Received binary: %(msg.bytes.count) bytes")
ws.sendBinary(msg.bytes)
}
}
System.print("Client disconnected\n")
}