summaryrefslogtreecommitdiff
path: root/web/socket/client.scm
diff options
context:
space:
mode:
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)))))