From 6142d3676a576d35fed0dab7a7cd6b05487aec7f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 1 Feb 2021 00:05:26 +0100 Subject: main: Ensure WebSocket connection is always closed Create a new `with-websocket-connection` decorator that starts and closes a WebSocket connection to the given `client` around the body forms. Thanks to Practical Common Lisp's "The Special Operators" chapter (http://www.gigamonkeys.com/book/the-special-operators.html#unwinding-the-stack) for introducing me to `unwind-protect`. Couldn't figure out how to get the new macro to auto-indent properly with Vlime, so ended up manually indenting it. --- l/src/main.lisp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'l/src/main.lisp') diff --git a/l/src/main.lisp b/l/src/main.lisp index d1336d7..969e44f 100644 --- a/l/src/main.lisp +++ b/l/src/main.lisp @@ -5,15 +5,12 @@ (defvar *wg* (wait-group:make-wait-group)) (defun main () - (wsd:start-connection *client*) + (with-websocket-connection (*client*) + (wsd:on :message *client* #'ws-on-message) - (wsd:on :message *client* #'ws-on-message) + (websocket-send *client* (target-get-targets-msg 1)) - (websocket-send *client* (target-get-targets-msg 1)) - - (wait-group:wait *wg*) - - (wsd:close-connection *client*)) + (wait-group:wait *wg*))) (defun target-get-targets-msg (call-id) (jsown:to-json -- cgit v1.2.3