summaryrefslogtreecommitdiff
path: root/web/socket/client.scm
diff options
context:
space:
mode:
authorMatthew Wolff <matthewjwolff@gmail.com>2024-05-08 19:38:02 -0500
committerDavid Thompson <dthompson2@worcester.edu>2024-05-09 07:51:56 -0400
commit438d87675d9ef18695475685ff36ff75a5506466 (patch)
treed2d49e3d0f19377b251bd27f3e1a9e52cbae79f2 /web/socket/client.scm
parent0ab74ba68c57f7b16832c3549d9939a3c78d7da4 (diff)
Distinguish close frames from timeouts.
Diffstat (limited to 'web/socket/client.scm')
-rw-r--r--web/socket/client.scm36
1 files changed, 21 insertions, 15 deletions
diff --git a/web/socket/client.scm b/web/socket/client.scm
index 4510493..966e3f7 100644
--- a/web/socket/client.scm
+++ b/web/socket/client.scm
@@ -211,18 +211,24 @@ connected to."
(define* (websocket-receive ws #:key timeout)
"Read data from the server that WS is connected to. Returns a string
-if text data was received, a bytevector if binary data was received,
-or #f if the WebSocket connection was closed or TIMEOUT seconds
-elapsed without receiving a data frame."
- (let* ((socket (websocket-socket ws))
- (frame (read-data-frame socket #:timeout timeout)))
- (cond ((not frame)
- (when (port-closed? socket)
- ;; EOF - clean up the websocket.
- (close-port (websocket-entropy-port ws))
- (set-websocket-state! ws 'closed))
- #f)
- ((binary-frame? frame)
- (frame-data frame))
- (else
- (text-frame->string frame)))))
+if text data was received, a bytevector if binary data was received, a
+pair of (code . reason) if the WebSocket was closed, or #f if TIMEOUT
+seconds elapsed without receiving a data frame."
+ (let ((socket (websocket-socket ws)))
+ ;; For server initiated disconnects and timeouts.
+ (define (maybe-close)
+ (when (port-closed? socket)
+ ;; EOF - clean up the websocket.
+ (close-port (websocket-entropy-port ws))
+ (set-websocket-state! ws 'closed)))
+ (match (read-data-frame socket #:timeout timeout)
+ (#f ; timeout
+ (maybe-close)
+ #f)
+ ((? close-frame? frame)
+ (maybe-close)
+ (close-frame->status frame))
+ ((? binary-frame? frame)
+ (frame-data frame))
+ ((? text-frame? frame)
+ (text-frame->string frame)))))