Skip to content

Commit e966c5d

Browse files
kwrooijennnicandro
authored andcommitted
Read websocket frame until completed
1 parent 7bd6390 commit e966c5d

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

jupyter-server-kernel.el

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ Call the next method if ARGS does not contain :server."
193193
(jupyter-run-with-io pub
194194
(jupyter-publish 'reauthenticate))))
195195

196+
(defvar jupyter-server-kernel-frame-acc "")
197+
198+
(defun jupyter-server-kernel-parse-frame (frame)
199+
"Accumulate the WebSocket frame data and return the full response when complete.
200+
Return nil if the frame is not yet complete."
201+
(let ((payload (websocket-frame-payload frame)))
202+
(setq jupyter-server-kernel-frame-acc (concat jupyter-server-kernel-frame-acc payload))
203+
(if (websocket-frame-completep frame)
204+
(let ((full-response jupyter-server-kernel-frame-acc))
205+
(setq jupyter-server-kernel-frame-acc "")
206+
full-response)
207+
nil)))
208+
196209
(cl-defmethod jupyter-websocket-io ((kernel jupyter-server-kernel))
197210
"Return a list representing an IO connection to KERNEL.
198211
The list is composed of two elements (IO-PUB ACTION-SUB), IO-PUB
@@ -274,16 +287,16 @@ this case FN will be evaluated on KERNEL."
274287
;; TODO: on-error publishes to status-pub
275288
:on-message
276289
(lambda (_ws frame)
277-
(pcase (websocket-frame-opcode frame)
278-
((or 'text 'binary)
279-
(let ((msg (jupyter-read-plist-from-string
280-
(websocket-frame-payload frame))))
281-
(jupyter-run-with-io kernel-io
282-
(jupyter-publish (cons 'message msg)))))
283-
(_
284-
(jupyter-run-with-io status-pub
285-
(jupyter-publish
286-
(list 'error (websocket-frame-opcode frame))))))))))
290+
(when-let ((response (jupyter-server-kernel-parse-frame frame)))
291+
(pcase (websocket-frame-opcode frame)
292+
((or 'text 'binary 'continuation)
293+
(let ((msg (jupyter-read-plist-from-string response)))
294+
(jupyter-run-with-io kernel-io
295+
(jupyter-publish (cons 'message msg)))))
296+
(_
297+
(jupyter-run-with-io status-pub
298+
(jupyter-publish
299+
(list 'error (websocket-frame-opcode frame)))))))))))
287300
(ws (prog1 (funcall make-websocket)
288301
(jupyter-run-with-io reauth-pub
289302
(jupyter-subscribe

0 commit comments

Comments
 (0)