This commit is contained in:
Nicholas Kariniemi 2014-10-10 19:56:04 +00:00
commit b9de047b7a
3 changed files with 24 additions and 8 deletions

View file

@ -18,7 +18,9 @@
[clj-webdriver "0.6.1" :exclusions [org.clojure/core.cache]] [clj-webdriver "0.6.1" :exclusions [org.clojure/core.cache]]
[sablono "0.2.17"] [sablono "0.2.17"]
[cljs-uuid "0.0.4"] [cljs-uuid "0.0.4"]
[net.polyc0l0r/hasch "0.2.3"]] [net.polyc0l0r/hasch "0.2.3"]
[com.cognitect/transit-clj "0.8.259"]
[com.cognitect/transit-cljs "0.8.188"]]
:profiles {:uberjar {:aot :all} :profiles {:uberjar {:aot :all}
:dev {:dependencies [[midje "1.6.3"]]}} :dev {:dependencies [[midje "1.6.3"]]}}
:min-lein-version "2.1.2" :min-lein-version "2.1.2"

View file

@ -1,18 +1,31 @@
(ns grub.websocket (ns grub.websocket
(:require [grub.db :as db] (:require [grub.db :as db]
[org.httpkit.server :as httpkit] [org.httpkit.server :as httpkit]
[clojure.core.async :as a :refer [<! >! chan go]])) [clojure.core.async :as a :refer [<! >! chan go]]
[cognitect.transit :as t])
(:import [java.io ByteArrayInputStream ByteArrayOutputStream]))
(defn disconnected [status ws-channel to from] (defn disconnected [status ws-channel to from]
(println "Client disconnected:" (.toString ws-channel) "with status" status) (println "Client disconnected:" (.toString ws-channel) "with status" status)
(a/close! to) (a/close! to)
(a/close! from)) (a/close! from))
(defn write-msg [msg]
(let [out (ByteArrayOutputStream. 4096)
writer (t/writer out :json)]
(t/write writer msg)
(.toString out)))
(defn read-msg [msg]
(let [in (ByteArrayInputStream. (.getBytes msg))
reader (t/reader in :json)]
(t/read reader)))
(defn add-connected-client! [ws-channel to from] (defn add-connected-client! [ws-channel to from]
(println "Client connected:" (.toString ws-channel)) (println "Client connected:" (.toString ws-channel))
(a/go-loop [] (if-let [event (<! to)] (a/go-loop [] (if-let [event (<! to)]
(do (httpkit/send! ws-channel (pr-str event)) (do (httpkit/send! ws-channel (write-msg event))
(recur)) (recur))
(httpkit/close ws-channel))) (httpkit/close ws-channel)))
(httpkit/on-receive ws-channel #(a/put! from (read-string %))) (httpkit/on-receive ws-channel #(a/put! from (read-msg %)))
(httpkit/on-close ws-channel #(disconnected % ws-channel to from))) (httpkit/on-close ws-channel #(disconnected % ws-channel to from)))

View file

@ -1,20 +1,21 @@
(ns grub.websocket (ns grub.websocket
(:require [cljs.core.async :as a :refer [<! >! chan]] (:require [cljs.core.async :as a :refer [<! >! chan]]
[cljs.reader]
goog.net.WebSocket goog.net.WebSocket
goog.events.EventHandler goog.events.EventHandler
goog.events.EventTarget goog.events.EventTarget
[hasch.core :as hasch]) [cognitect.transit :as t])
(:require-macros [cljs.core.async.macros :refer [go go-loop]] (:require-macros [cljs.core.async.macros :refer [go go-loop]]
[grub.macros :refer [log logs]])) [grub.macros :refer [log logs]]))
(def server-url (str "ws://" (.-host (.-location js/document)))) (def server-url (str "ws://" (.-host (.-location js/document))))
(def pending-msg (atom nil)) (def pending-msg (atom nil))
(def reader (t/reader :json))
(def writer (t/writer :json))
(defn send-pending-msg [websocket] (defn send-pending-msg [websocket]
(when (and (.isOpen websocket) (when (and (.isOpen websocket)
(not (nil? @pending-msg))) (not (nil? @pending-msg)))
(.send websocket (pr-str @pending-msg)) (.send websocket (t/write writer @pending-msg))
(reset! pending-msg nil))) (reset! pending-msg nil)))
(defn on-connected [websocket event] (defn on-connected [websocket event]
@ -22,7 +23,7 @@
(send-pending-msg websocket)) (send-pending-msg websocket))
(defn read-msg [msg] (defn read-msg [msg]
(cljs.reader/read-string (.-message msg))) (t/read reader (.-message msg)))
(defn connect-client! [in out] (defn connect-client! [in out]
(let [handler (goog.events.EventHandler.) (let [handler (goog.events.EventHandler.)