Skip to content

Commit

Permalink
Emit a socket closed event in a non-async socket loop
Browse files Browse the repository at this point in the history
Normally when using an async web server, websocket-driver handles broken
connections by attempting to write to a stream, and if that fails, it
closes the socket and emits the close event. In the case of a non-async
webserver like Hunchentoot, a loop form checks the stream's status
manually, and then closes the socket once the loop exits. But in this
case, it doesn't emit the close event. This PR adds the event to this
case as well.
  • Loading branch information
SuperDisk committed Jul 8, 2024
1 parent f72e1dc commit c7323af
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/ws/server.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

(defmethod start-connection ((server server) &key)
(unless (eq (ready-state server) :connecting)
(return-from start-connection))
(return-from start-connection))

(let ((socket (socket server)))
(setf (read-callback socket)
Expand All @@ -81,7 +81,8 @@
while frame
do (funcall (read-callback socket) frame))
(close-connection server)
(setf (ready-state server) :closed)))))
(setf (ready-state server) :closed)
(wsd:emit :close server :code 1006 :reason "websocket connection closed")))))

(defmethod close-connection ((server server) &optional (reason "") (code (error-code :normal-closure)))
(setf (ready-state server) :closing)
Expand All @@ -101,11 +102,11 @@
:code code
:masking nil)))
(handler-case
(write-sequence-to-socket (socket server) frame
:callback callback)
(write-sequence-to-socket (socket server) frame
:callback callback)
(error ()
(setf (ready-state server) :closed)
(wsd:emit :close server :code 1006 :reason "websocket connection closed")))))
(wsd:emit :close server :code 1006 :reason "websocket connection closed")))))

(defmethod send-handshake-response ((server server) &key callback)
(let ((socket (socket server))
Expand Down

0 comments on commit c7323af

Please sign in to comment.