Harlan Iverson 8 лет назад
Родитель
Сommit
231f585b68

+ 4 - 0
project.clj

@@ -27,6 +27,9 @@
 
             :versions {com.github.harlanji.clojure-stack/core "0.0.1-SNAPSHOT"
                        com.github.harlanji.clojure-stack/pubsub-service "0.0.1-SNAPSHOT"
+                       com.analogzen.clojure-stack/web-client "0.0.1-SNAPSHOT"
+
+
                        ; idea: automatically insert module versions
                        ; https://github.com/jcrossley3/lein-modules/issues/29
                        lein-objcbuild "0.1.10"
@@ -38,6 +41,7 @@
                        :clj-objc1.7 "1.7.0-RC1" ; even if stuff is beta2 it should be okay, since it's not AOT'd
                        :component/clojure1.2+ "0.2.4-harlanji-SNAPSHOT" ; ios, until [galdolber/clojure-objc "1.7.0"]
                        :component/clojure1.7+ "0.3.1" ; other
+                       :sente1.7 "1.7.0"
                        reagent "0.5.1"
                        speclj "3.3.1"}
             }

+ 4 - 2
web-backend/project.clj

@@ -1,9 +1,11 @@
 (defproject com.analogzen.clojure-stack/web-backend "0.0.1-SNAPSHOT"
             :dependencies [[org.clojure/clojure :clj1.8]
                            [ring "1.4.0"]
-                           [com.analogzen.clojure-stack/web-client "0.0.1-SNAPSHOT"]
+                           [ring/ring-anti-forgery "1.0.0"]
+                           [com.analogzen.clojure-stack/web-client _]
                            [http-kit "2.1.18"]
-                           [environ "1.0.2"]]
+                           [environ "1.0.2"]
+                           [com.taoensso/sente :sente1.7]]
             :plugins [[lein-modules "0.3.11"]]
 
             :main example.main

+ 41 - 3
web-backend/src/example/core.clj

@@ -1,13 +1,51 @@
 (ns example.core
     (:require [ring.middleware.resource :refer [wrap-resource]]
-              [org.httpkit.server :refer [run-server]]))
+              [org.httpkit.server :refer [run-server]]
+              [taoensso.sente :as sente]
+              [taoensso.sente.server-adapters.http-kit :refer [sente-web-server-adapter]]
+              [ring.middleware.keyword-params :refer [wrap-keyword-params]]
+              [ring.middleware.params :refer [wrap-params]]
+              [ring.middleware.anti-forgery :refer [wrap-anti-forgery]]
+              [ring.middleware.session :refer [wrap-session]]
+              [clojure.core.async :refer [go <!]]
+              ))
 
 (defn say-hi [req]
       (when (.startsWith (:uri req) "/hi")
             {:status 200 :body "hi" :content-type "text/plain"}))
 
-(def app (-> say-hi
-             (wrap-resource "public")))
+(defn sente-handler [uri]
+  (let [{:keys [ch-recv
+                send-fn
+                ajax-post-fn
+                ajax-get-or-ws-handshake-fn
+                connected-uids]}
+        (sente/make-channel-socket! sente-web-server-adapter {})]
+    (go (while true
+          (let [event (<! ch-recv)]
+            (println "event:" event))))
+    (fn [req]
+      (when (= (:uri req) uri)
+        (println "Starting sente session.")
+        (case (:request-method req)
+          :post (ajax-post-fn req)
+          :get (ajax-get-or-ws-handshake-fn req)
+          :else nil)))))
+
+(defn wrap-cors-all [handler]
+  (fn [req]
+    (update (handler req) :headers #(assoc % "Access-Control-Allow-Origin" "*"))))
+
+(def app (let [chsk (sente-handler "/chsk")]
+           (-> #(or (say-hi %)
+                    (chsk %))
+               (wrap-resource "public")
+               wrap-cors-all
+               wrap-keyword-params
+               wrap-params
+               wrap-anti-forgery
+               wrap-session
+               )))
 
 (defn start! [port]
       (println "server on. port: " port)

+ 2 - 0
web-client/project.clj

@@ -2,6 +2,8 @@
             :dependencies [[org.clojure/clojure :clj1.8]
                            [org.clojure/clojurescript :cljs1.8]
                            [com.stuartsierra/component :component/clojure1.7+]
+                           [com.taoensso/sente :sente1.7]
+
                            ;[reagent "0.5.1"
                            ; :exclusions [org.clojure/tools.reader]]
                            ]

+ 2 - 0
web-client/resources/public/index.html

@@ -2,6 +2,8 @@
 <html>
 <head></head>
 <body>
+hi! Check out the console.
+
 <script src="gen/js/main.js" type="text/javascript"></script>
 <script type="text/javascript">example.core.start_BANG_(window);</script>
 </body>

+ 34 - 4
web-client/src/example/core.cljs

@@ -1,18 +1,48 @@
 (ns example.core
-  (:require [com.stuartsierra.component :as component]))
+  (:require [com.stuartsierra.component :as component]
+            [cljs.core.async :refer [<! >! put! chan] :as async]
+            [taoensso.sente  :as sente :refer [cb-success?]]
+            )
+  (:require-macros [cljs.core.async.macros :refer [go go-loop] :as asyncm]))
 
 (.info js/console "Prod loaded.")
 
-(defrecord App []
+(defrecord App [sente]
   component/Lifecycle
   (start [component]
-    (.info js/console "App started.")
+    (.info js/console "App started. sente=" sente)
     component)
   (stop [component]
     (.info js/console "App stopped.")
     component))
 
-(defonce system (atom (component/system-map :app (map->App {}))))
+(defrecord Sente [client-id
+                  sente-chsk]
+  component/Lifecycle
+  (start [c]
+    (let [sente-chsk (sente/make-channel-socket! "/chsk" {:type :auto
+                                                          :client-id client-id})]
+      (go (while true
+            (let [event (<! (:ch-recv sente-chsk))]
+              (.info js/console (str "Got event! " event)))))
+      (.info js/console "Sente started.")
+
+      (.setTimeout js/window #((:send-fn sente-chsk) [:client/hi]) 500)
+
+      (assoc c :sente-chsk sente-chsk)))
+  (stop [c]
+    (when (:sente-chsk c)
+      (.info js/console "Sente stopped.")
+      (dissoc c :sente-chsk))))
+
+(defonce system (atom (component/system-map :sente (map->Sente {:client-id "1234"})
+                                            :app (component/using
+                                                   (map->App {})
+                                                   {:sente :sente}))))
+
+(defn send-message [msg]
+  (let [sente-chsk (get-in @system [:sente :sente-chsk])]
+    ))
 
 (defn start! [window]
   (.info js/console "Starting...")