-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agent heartbeat message -> event connection is not closed !
- Loading branch information
Showing
3 changed files
with
22 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
(ns swarmpit.handler-events | ||
(:require [org.httpkit.server :refer [run-server with-channel on-close send! close]] | ||
[immutant.scheduling :refer :all] | ||
[cheshire.core :refer [generate-string]] | ||
[swarmpit.handler :refer [dispatch resp-accepted resp-error]])) | ||
|
||
(def channel-hub (atom {})) | ||
|
||
(defn broadcast [message] | ||
(doseq [channel (keys @channel-hub)] | ||
(send! channel message false))) | ||
(defn broadcast [data] | ||
(let [message (str "data: " (generate-string data) "\n\n")] | ||
(doseq [channel (keys @channel-hub)] | ||
(send! channel message false)))) | ||
|
||
(defmethod dispatch :events [_] | ||
(fn [request] | ||
(with-channel request channel | ||
(send! channel {:status 200 | ||
:headers {"Content-Type" "text/event-stream" | ||
"Cache-Control" "no-cache" | ||
"Connection" "keep-alive"} | ||
:headers {"Content-Type" "text/event-stream" | ||
"Cache-Control" "no-cache" | ||
"Connection" "keep-alive"} | ||
:body ":ok\n\n"} false) | ||
(swap! channel-hub assoc channel request) | ||
(on-close channel (fn [_] | ||
(swap! channel-hub dissoc channel)))))) | ||
|
||
(defmethod dispatch :event-push [_] | ||
(fn [{:keys [params]} ] | ||
(fn [{:keys [params]}] | ||
(if (some? params) | ||
(do | ||
(broadcast (str "data: " (generate-string params) "\n\n")) | ||
(broadcast params) | ||
(resp-accepted (str "Broadcasted to " (count @channel-hub) " clients"))) | ||
(resp-error 400 "No data send")))) |