diff --git a/.gitignore b/.gitignore index cc2ff39..982dbca 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ pom.xml.asc .lein-failures .lein-plugins .lein-repl-history +src/prod diff --git a/project.clj b/project.clj index d2917e2..24c4b76 100644 --- a/project.clj +++ b/project.clj @@ -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}}}) diff --git a/src/clj/grub/websocket.clj b/src/clj/grub/websocket.clj index 7a6ed71..b56b504 100644 --- a/src/clj/grub/websocket.clj +++ b/src/clj/grub/websocket.clj @@ -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)))) diff --git a/src/cljs/grub/core.cljs b/src/cljs/grub/core.cljs index ab4ca66..877d552 100644 --- a/src/cljs/grub/core.cljs +++ b/src/cljs/grub/core.cljs @@ -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) diff --git a/src/cljs/grub/websocket.cljs b/src/cljs/grub/websocket.cljs index c264b5e..6c42fd0 100644 --- a/src/cljs/grub/websocket.cljs +++ b/src/cljs/grub/websocket.cljs @@ -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 (! 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)))