feat: add SSE URL construction and connection management
This commit is contained in:
@@ -71,5 +71,34 @@ Buffers incoming STRING and processes complete events."
|
|||||||
This function will be overridden in noise-receive to process envelopes."
|
This function will be overridden in noise-receive to process envelopes."
|
||||||
(message "SSE event received: %s" event))
|
(message "SSE event received: %s" event))
|
||||||
|
|
||||||
|
(defvar noise-signal-cli-address)
|
||||||
|
(defvar noise-account)
|
||||||
|
|
||||||
|
(defun noise-sse--build-url ()
|
||||||
|
"Build the SSE endpoint URL for signal-cli."
|
||||||
|
(let ((base (concat noise-signal-cli-address "/api/v1/events")))
|
||||||
|
(if (and (boundp 'noise-account) noise-account)
|
||||||
|
(concat base "?account=" (url-hexify-string noise-account))
|
||||||
|
base)))
|
||||||
|
|
||||||
|
(defun noise-sse--sentinel (proc status)
|
||||||
|
"Process sentinel for SSE connection.
|
||||||
|
Handles connection close and triggers reconnection."
|
||||||
|
(when (or (equal status "finished\n")
|
||||||
|
(string-match-p "failed" status)
|
||||||
|
(string-match-p "deleted" status))
|
||||||
|
(message "Noise: SSE connection lost, reconnecting in %ds..."
|
||||||
|
noise-sse--reconnect-delay)
|
||||||
|
(setq noise-sse--process nil)
|
||||||
|
(noise-sse--schedule-reconnect)))
|
||||||
|
|
||||||
|
(defun noise-sse--schedule-reconnect ()
|
||||||
|
"Schedule a reconnection attempt after the current delay."
|
||||||
|
(when noise-sse--reconnect-timer
|
||||||
|
(cancel-timer noise-sse--reconnect-timer))
|
||||||
|
(setq noise-sse--reconnect-timer
|
||||||
|
(run-with-timer noise-sse--reconnect-delay nil
|
||||||
|
#'noise-sse-connect)))
|
||||||
|
|
||||||
(provide 'noise-sse)
|
(provide 'noise-sse)
|
||||||
;;; noise-sse.el ends here
|
;;; noise-sse.el ends here
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
(require 'ert)
|
(require 'ert)
|
||||||
(require 'noise-sse)
|
(require 'noise-sse)
|
||||||
|
|
||||||
|
(defvar noise-signal-cli-address)
|
||||||
|
(defvar noise-account)
|
||||||
|
|
||||||
(ert-deftest noise-sse--parse-event-basic ()
|
(ert-deftest noise-sse--parse-event-basic ()
|
||||||
"Parse a basic SSE event with data field."
|
"Parse a basic SSE event with data field."
|
||||||
(let ((event "data: {\"type\":\"message\",\"content\":\"hello\"}\n\n"))
|
(let ((event "data: {\"type\":\"message\",\"content\":\"hello\"}\n\n"))
|
||||||
@@ -76,5 +79,21 @@
|
|||||||
(noise-sse--filter nil "}\n\n")
|
(noise-sse--filter nil "}\n\n")
|
||||||
(should (= 1 (length processed-events)))))))
|
(should (= 1 (length processed-events)))))))
|
||||||
|
|
||||||
|
(ert-deftest noise-sse--build-url ()
|
||||||
|
"Build SSE endpoint URL from base address."
|
||||||
|
(let ((noise-signal-cli-address "http://localhost:8080")
|
||||||
|
(noise-account "+15551234567"))
|
||||||
|
(let ((url (noise-sse--build-url)))
|
||||||
|
(should (string-match-p "/api/v1/events" url))
|
||||||
|
(should (string-match-p "account=%2B15551234567" url)))))
|
||||||
|
|
||||||
|
(ert-deftest noise-sse--build-url-no-account ()
|
||||||
|
"Build SSE endpoint URL without account parameter."
|
||||||
|
(let ((noise-signal-cli-address "http://localhost:8080")
|
||||||
|
(noise-account nil))
|
||||||
|
(let ((url (noise-sse--build-url)))
|
||||||
|
(should (string-match-p "/api/v1/events" url))
|
||||||
|
(should-not (string-match-p "account=" url)))))
|
||||||
|
|
||||||
(provide 'noise-sse-test)
|
(provide 'noise-sse-test)
|
||||||
;;; noise-sse-test.el ends here
|
;;; noise-sse-test.el ends here
|
||||||
|
|||||||
Reference in New Issue
Block a user