Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix case where websocket collision crashes frames #227

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions org-roam-ui.el
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,35 @@ This serves the web-build and API over HTTP."
:group 'org-roam-ui
:init-value nil
(cond
(org-roam-ui-mode
((and org-roam-ui-mode (not (org-roam-ui-server-runningp)))
(message "org-roam-ui server :: starting up...")
;;; check if the default keywords actually exist on `orb-preformat-keywords'
;;; else add them
(setq-local httpd-port org-roam-ui-port)
(setq httpd-root org-roam-ui-app-build-dir)
(httpd-start)
(setq org-roam-ui-ws-server
(websocket-server
35903
:host 'local
:on-open #'org-roam-ui--ws-on-open
:on-message #'org-roam-ui--ws-on-message
:on-close #'org-roam-ui--ws-on-close))
(when org-roam-ui-open-on-start (org-roam-ui-open)))
(t
(progn
(websocket-server-close org-roam-ui-ws-server)
(httpd-stop)
(remove-hook 'after-save-hook #'org-roam-ui--on-save)
(org-roam-ui-follow-mode -1)))))
(setq-local httpd-port org-roam-ui-port)
(setq httpd-root org-roam-ui-app-build-dir)
(httpd-start)
(setq org-roam-ui-ws-server
(websocket-server
35903
:host 'local
:on-open #'org-roam-ui--ws-on-open
:on-message #'org-roam-ui--ws-on-message
:on-close #'org-roam-ui--ws-on-close))
(when org-roam-ui-open-on-start (org-roam-ui-open)))
(org-roam-ui-mode
(message (format "org-roam-ui server already runnning.")))
(t
(progn
(message "org-roam-ui server :: shutting down...")
(if org-roam-ui-ws-server
(websocket-server-close org-roam-ui-ws-server))
(httpd-stop)
(remove-hook 'after-save-hook #'org-roam-ui--on-save)
(org-roam-ui-follow-mode -1)))))

(defun org-roam-ui-server-runningp ()
"True if the server is running or listening, otherwise false."
(and org-roam-ui-ws-server (member (process-status org-roam-ui-ws-server) '(listen run)) t))

(defun org-roam-ui--ws-on-open (ws)
"Open the websocket WS to org-roam-ui and send initial data."
Expand Down