Production server

This commit is contained in:
Nicholas Kariniemi 2013-08-25 00:32:56 +03:00
parent 0cc95f0829
commit 26035a5174
5 changed files with 18 additions and 10 deletions

1
.gitignore vendored
View file

@ -10,3 +10,4 @@ pom.xml.asc
.lein-failures
.lein-plugins
.lein-repl-history
src/prod

View file

@ -28,7 +28,7 @@
:optimizations :whitespace
:pretty-print true}
:notify-command run-specs}
:prod {:source-paths ["src/cljs"]
:prod {:source-paths ["src/cljs" "src/prod"]
:compiler {:output-to "public/js/grub.js"
:optimizations :simple}}
:test-commands {"test" run-specs}}})

View file

@ -47,6 +47,7 @@
c (chan)]
(swap! ws-channels conj {:id ws-channel-id :channel c})
(println "Channel connected:" (.toString ws-channel))
(println "Request:" request)
(httpkit/on-receive ws-channel #(add-incoming-event % ws-channel-id))
(push-current-grubs-to-client c ws-channel)
(push-received-events-to-client c ws-channel))))

View file

@ -15,7 +15,9 @@
(defn init []
(view/init)
(ws/connect-to-server)
(if-let [server-url (when grub.production grub.production/server-url)]
(do (logs "prod") (ws/connect-to-server server-url 80))
(do (logs "dev") (ws/connect-to-server "localhost" 3000)))
(handle-grub-events))
(init)

View file

@ -3,8 +3,8 @@
:refer [fan-in fan-out event-chan filter-chan do-chan do-chan! map-chan]]
[cljs.core.async :refer [<! >! >!! chan close! timeout]]
[cljs.reader])
(:require-macros [grub.macros :refer [log logs go-loop]]
[cljs.core.async.macros :refer [go]]))
(:require-macros [cljs.core.async.macros :refer [go]]
[grub.macros :refer [log logs go-loop]]))
(def incoming-events (chan))
(def outgoing-events (chan))
@ -14,15 +14,19 @@
(defn handle-incoming-events []
(go-loop
(let [event (<! incoming-events)]
(logs "Sending event:" event)
(.send @websocket* event))))
(defn handle-outgoing-events []
(aset @websocket* "onmessage" (fn [event]
(let [grub-event (cljs.reader/read-string (.-data event))]
(logs "Received:" grub-event)
(go (>! outgoing-events grub-event))))))
(defn connect-to-server []
(reset! websocket* (js/WebSocket. "ws://localhost:3000/ws"))
(handle-incoming-events)
(handle-outgoing-events))
(defn connect-to-server [url port]
(let [full-url (str "ws://" url ":" port "/ws")]
(reset! websocket* (js/WebSocket. full-url))
(aset @websocket* "onopen" (fn [event] (log "Connected:" event)))
(aset @websocket* "onclose" (fn [event] (log "Connection closed:" event)))
(aset @websocket* "onerror" (fn [event] (log "Connection error:" event)))
(handle-incoming-events)
(handle-outgoing-events)))