Restrukturiranje projekta, socket fix

master
Jure Podgoršek 2018-08-09 11:28:37 +02:00
parent d421a03cc1
commit 29da6fa0d3
11 changed files with 271 additions and 189 deletions

View File

@ -13,7 +13,9 @@
[org.clojure/core.async "0.4.474"]
[jarohen/chord "0.8.1"]
[ring "1.7.0-RC1"]
[compojure "1.6.1"]]
[compojure "1.6.1"]
[reagent "0.8.1"]
[secretary "1.2.3"]]
:plugins [[lein-figwheel "0.5.16"]
[lein-cljsbuild "1.1.7" :exclusions [[org.clojure/clojure]]]]
@ -24,15 +26,7 @@
[{:id "dev"
:source-paths ["src"]
;; The presence of a :figwheel configuration here
;; will cause figwheel to inject the figwheel client
;; into your build
:figwheel {:on-jsload "sliva.core/on-js-reload"
;; :open-urls will pop open your application
;; in the default browser once Figwheel has
;; started and compiled your application.
;; Comment this out once it no longer serves you.
:open-urls ["http://localhost:3449/index.html"]}
:figwheel {:on-jsload "sliva.core/init-app"}
:compiler {:main sliva.core
:asset-path "js/compiled/out"
@ -67,7 +61,7 @@
;; doesn't work for you just run your own server :) (see lein-ring)
:ring-handler sliva.server/routes
:ring-handler sliva.server/handler
;; To be able to open files in your editor from the heads up display
;; you will need to put a script on your path.

View File

@ -10,7 +10,7 @@
</head>
<body style="margin: 0">
<div id="container"></div>
<script type="text/javascript" src="js/three.min.js"></script>
<!-- script type="text/javascript" src="js/three.min.js"></script-->
<script src="js/compiled/sliva.js" type="text/javascript"></script>
</body>
</html>

View File

@ -1,144 +1,32 @@
(ns sliva.core
(:require [sliva.ctl :refer [websocket-init vtic]]
[cljs.core.async :refer [>!]])
(:require
[secretary.core :as secretary :refer-macros [defroute]]
[reagent.core :as reagent]
[sliva.data :refer [appstate]]
[sliva.routes :refer [app-routes]]
[sliva.pages.hub :refer [hub]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
(enable-console-print!)
(console.log "Hello, Sky!")
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
;; ☭☭☭☭ Parametri razni ☭☭☭☭ ;;
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
;; Page switching
(defmulti current-page #(@appstate :page))
(defmethod current-page :hub [] hub)
(def odmik-kamere 100)
(def rotacija-kamere 1)
(def FOV 140)
(def lik-sirina 2)
(def obj-limit 1000)
;;(defn on-js-reload []
;; (stop-render)
;; (start-render)
;; (websocket-init))
(def objekti (atom []))
(def stevec (atom 0))
;; On figwheel reload do this (reload routes, render currently active page)
(defn init-app []
(app-routes)
(reagent/render [current-page] (.getElementById js/document "container")))
(def rotacija-x 0.006)
(def rotacija-y 0.001)
(def rotacija-z 0.003)
(def zamik-barve 0.0000666)
(def zacetna-barva 0.333)
(def saturacija 1)
(def svetlost 0.4)
(def w-diff 0.5)
(def gostota-obj 2)
;; Inicializacija ;;
(def scena (THREE.Scene.))
(def kamera (THREE.PerspectiveCamera. FOV (/ (.-innerWidth js/window)
(.-innerHeight js/window)) 0.1 2000))
(aset kamera "position" "z" odmik-kamere)
(aset kamera "aspect" (/ (.-innerWidth js/window)
(.-innerHeight js/window)))
(.updateProjectionMatrix kamera)
(def izris (THREE.WebGLRenderer. (js-obj "alpha" true)))
(.setSize izris (.-innerWidth js/window) (.-innerHeight js/window))
;; Crno ozadje
(.setClearColor izris 0x000000 1)
(defn dodaj-obj [sirina]
(let [barva (js/THREE.Color.)
mat (js/THREE.LineBasicMaterial.)
geo (js/THREE.Geometry.)
koti (.-vertices geo)
zamik (/ sirina 2)
-zamik (* -1 zamik)]
;; Nastavi barvo novega objekta
(.setHSL barva (* @stevec zacetna-barva) saturacija svetlost)
(aset mat "color" barva)
;; Kvadratek (za nov objekt)
(.push koti (js/THREE.Vector3. -zamik 0 0) (js/THREE.Vector3. 0 zamik 0))
(.push koti (js/THREE.Vector3. -zamik 0 0) (js/THREE.Vector3. 0 -zamik 0))
(.push koti (js/THREE.Vector3. zamik 0 0) (js/THREE.Vector3. 0 zamik 0))
(.push koti (js/THREE.Vector3. zamik 0 0) (js/THREE.Vector3. 0 -zamik 0))
(let [obj (js/THREE.Line. geo mat (.-LineSegments js/THREE))
novi-objekti (conj (take obj-limit @objekti)
obj)]
(.add scena obj)
;; pucaj sceno ane
(reset! objekti novi-objekti))))
(defn obj-anim [obj]
(.rotateY obj rotacija-y)
(.rotateZ obj rotacija-z)
(.rotateX obj rotacija-x)
(let [new-scale (+ (aget obj "scale" "x")
w-diff)]
(aset obj "scale" "x" new-scale)
(aset obj "scale" "y" new-scale)
(aset obj "scale" "z" new-scale))
(.offsetHSL (aget obj "material" "color") zamik-barve 0 0))
(defn cam-rotate []
(.translateX kamera rotacija-kamere)
(.translateZ kamera (- odmik-kamere
(.sqrt js/Math (+ (.pow js/Math odmik-kamere 2)
(.pow js/Math rotacija-kamere 2)))))
(.lookAt kamera scena.position))
(defn render []
(.requestAnimationFrame js/window render)
(reset! stevec (inc @stevec))
(if (= 0 (rem @stevec gostota-obj))
(dodaj-obj lik-sirina))
(.render izris scena kamera)
(run! obj-anim @objekti)
(cam-rotate))
(defn start-render []
(let [container (.getElementById js/document "container")]
(.appendChild container (.-domElement izris))))
(defn stop-render []
(let [container (.getElementById js/document "container")]
(.removeChild container (.-firstChild container))))
;; Zacetek ;;
;; Vstopna tocka
(aset js/document "onreadystatechange"
(fn []
(if (= (.-readyState js/document) "complete")
(do
(start-render)
(render)
(websocket-init)))))
(defn on-js-reload []
(stop-render)
(start-render)
(websocket-init))
;; Hendlaj risajz
(defn on-window-resize[]
(let [sirina (.-innerWidth js/window)
visina (.-innerHeight js/window)]
(aset kamera "aspect" (/ sirina visina))
(.updateProjectionMatrix kamera)
(.setSize izris sirina visina)))
(.addEventListener js/window "resize" on-window-resize false)
(.addEventListener js/window "click" (fn []
(go []
(>! @vtic "ping"))))
(init-app))))

View File

@ -1,22 +0,0 @@
(ns sliva.ctl
(:require [chord.client :refer [ws-ch]]
[cljs.core.async :refer [<! >! put! close! chan]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
(def vtic (atom (chan)))
(defn websocket-init []
(console.log "init webscoket")
(go (let [{:keys [ws-channel error]} (<! (ws-ch "ws://localhost:3449/ws"))]
(if-not error
(do
(reset! vtic ws-channel)
(>! ws-channel "Hello server")
;; (go-loop []
;; (let [{:keys [message]} (<! ws-channel)]
;; (console.log "got msg" message)
;; (socket-handler message)
;; (recur))))
)
(js/console.log "Fejl websocket: " (pr-str error))))))

View File

@ -0,0 +1,10 @@
(ns sliva.data
(:require [cljs.core.async :refer [chan]]
[reagent.core :as reagent]))
;; App state atom
(def appstate
(reagent/atom
{:clients []}))
(def vtic (atom (chan)))

120
src/sliva/gfx.cljs 100644
View File

@ -0,0 +1,120 @@
(ns sliva.gfx)
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
;; ☭☭☭☭ Parametri razni ☭☭☭☭ ;;
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
(def odmik-kamere 100)
(def rotacija-kamere 1)
(def FOV 140)
(def lik-sirina 2)
(def obj-limit 1000)
(def objekti (atom []))
(def stevec (atom 0))
(def rotacija-x 0.006)
(def rotacija-y 0.001)
(def rotacija-z 0.003)
(def zamik-barve 0.0000666)
(def zacetna-barva 0.333)
(def saturacija 1)
(def svetlost 0.4)
(def w-diff 0.5)
(def gostota-obj 2)
;; Inicializacija ;;
(def scena (THREE.Scene.))
(def kamera (THREE.PerspectiveCamera. FOV (/ (.-innerWidth js/window)
(.-innerHeight js/window)) 0.1 2000))
(aset kamera "position" "z" odmik-kamere)
(aset kamera "aspect" (/ (.-innerWidth js/window)
(.-innerHeight js/window)))
(.updateProjectionMatrix kamera)
(def izris (THREE.WebGLRenderer. (js-obj "alpha" true)))
(.setSize izris (.-innerWidth js/window) (.-innerHeight js/window))
;; Crno ozadje
(.setClearColor izris 0x000000 1)
(defn dodaj-obj [sirina]
(let [barva (js/THREE.Color.)
mat (js/THREE.LineBasicMaterial.)
geo (js/THREE.Geometry.)
koti (.-vertices geo)
zamik (/ sirina 2)
-zamik (* -1 zamik)]
;; Nastavi barvo novega objekta
(.setHSL barva (* @stevec zacetna-barva) saturacija svetlost)
(aset mat "color" barva)
;; Kvadratek (za nov objekt)
(.push koti (js/THREE.Vector3. -zamik 0 0) (js/THREE.Vector3. 0 zamik 0))
(.push koti (js/THREE.Vector3. -zamik 0 0) (js/THREE.Vector3. 0 -zamik 0))
(.push koti (js/THREE.Vector3. zamik 0 0) (js/THREE.Vector3. 0 zamik 0))
(.push koti (js/THREE.Vector3. zamik 0 0) (js/THREE.Vector3. 0 -zamik 0))
(let [obj (js/THREE.Line. geo mat (.-LineSegments js/THREE))
novi-objekti (conj (take obj-limit @objekti)
obj)]
(.add scena obj)
;; pucaj sceno ane
(reset! objekti novi-objekti))))
(defn obj-anim [obj]
(.rotateY obj rotacija-y)
(.rotateZ obj rotacija-z)
(.rotateX obj rotacija-x)
(let [new-scale (+ (aget obj "scale" "x")
w-diff)]
(aset obj "scale" "x" new-scale)
(aset obj "scale" "y" new-scale)
(aset obj "scale" "z" new-scale))
(.offsetHSL (aget obj "material" "color") zamik-barve 0 0))
(defn cam-rotate []
(.translateX kamera rotacija-kamere)
(.translateZ kamera (- odmik-kamere
(.sqrt js/Math (+ (.pow js/Math odmik-kamere 2)
(.pow js/Math rotacija-kamere 2)))))
(.lookAt kamera scena.position))
(defn render []
(.requestAnimationFrame js/window render)
(reset! stevec (inc @stevec))
(if (= 0 (rem @stevec gostota-obj))
(dodaj-obj lik-sirina))
(.render izris scena kamera)
(run! obj-anim @objekti)
(cam-rotate))
(defn start-render []
(let [container (.getElementById js/document "container")]
(.appendChild container (.-domElement izris))))
(defn stop-render []
(let [container (.getElementById js/document "container")]
(.removeChild container (.-firstChild container))))
;; Hendlaj risajz
(defn on-window-resize[]
(let [sirina (.-innerWidth js/window)
visina (.-innerHeight js/window)]
(aset kamera "aspect" (/ sirina visina))
(.updateProjectionMatrix kamera)
(.setSize izris sirina visina)))
(.addEventListener js/window "resize" on-window-resize false)

View File

@ -0,0 +1,21 @@
(ns sliva.pages.hub
(:require [sliva.socket :refer [websocket-init click-test]]
[sliva.data :refer [appstate]]))
(websocket-init)
(click-test)
(defn hub []
[:div
[:h1 "Connected clients"]
[:table
[:thead
[:tr
[:th "Client ID"]
[:th "Something else?"]]]
[:tbody
(map (fn [cid]
[:tr {:key cid}
[:td cid]
[:td "connected"]])
(keys (:clients @appstate)))]]])

View File

@ -0,0 +1,20 @@
(ns sliva.routes
(:import goog.history.Html5History)
(:require [secretary.core :as secretary :refer-macros [defroute]]
[goog.events :as events]
[goog.history.EventType :as EventType]
[sliva.data :refer [appstate]]))
(defn hook-browser-navigation! []
(doto (Html5History.)
(events/listen
EventType/NAVIGATE
(fn [event]
(secretary/dispatch! (.-token event))))
(.setEnabled true)))
(defn app-routes []
(secretary/set-config! :prefix "#☭")
(defroute "/" []
(swap! appstate assoc :page :hub))
(hook-browser-navigation!))

View File

@ -3,32 +3,11 @@
[compojure.route :refer [resources]]
[ring.util.response :refer [resource-response]]
[ring.middleware.reload :refer [wrap-reload]]
[chord.http-kit :refer [with-channel]]
[org.httpkit.server :refer [run-server]]
[clojure.core.async :refer [chan <! <!! >! put! close! go go-loop]]
[clojure.string :as str]))
(defn socket-handler [request]
(let [cid (java.util.UUID/randomUUID)]
(with-channel request ws-ch
(go
(let [{:keys [message]} (<! ws-ch)]
(println "new connection - " cid)
(>! ws-ch (str "Hello " cid))
(go-loop []
(let [{:keys [message]} (<! ws-ch)]
(println "GOT MSG!" message)
(let [[cmd & args] (str/split message #":")]
(println "command" cmd "(" args ")")
(condp = cmd
"ping" (>! ws-ch "pong")
"Unknown command :/"))
(recur))))))))
;;(close! ws-ch)))))
[sliva.server.handlers :refer [socket-handler]]))
;; wat
(defroutes routes
(GET "/" [] (resource-response "index.html" {:root "public"}))
;;(GET "/" [] (resource-response "index.html" {:root "public"}))
(GET "/hello" [] "wat")
(GET "/ws" [] socket-handler)
(resources "/"))

View File

@ -0,0 +1,37 @@
(ns sliva.server.handlers
(:require [compojure.core :refer [defroutes GET]]
[compojure.route :refer [resources]]
[clojure.string :as str]
[clojure.core.async :refer [chan <! <!! >! put! close! go go-loop]]
[ring.util.response :refer [resource-response]]
[ring.middleware.reload :refer [wrap-reload]]
[chord.http-kit :refer [with-channel]]))
(def clients (atom {}))
(defn send-all [& args]
(println "sending to all" args (keys @clients))
(doseq [conn @clients]
(println "sending" args)
(put! (val conn) (str/join ":" args))))
(defn socket-handler [request]
(let [cid (java.util.UUID/randomUUID)]
(with-channel request ws-ch
(go
(let [{:keys [message]} (<! ws-ch)]
(println "new connection - " cid)
(>! ws-ch (str "Hello " cid))
(swap! clients assoc cid ws-ch)
(send-all "open" cid)
(go-loop []
(let [{:keys [message]} (<! ws-ch)]
(println "GOT MSG!" message)
(let [[cmd & args] (str/split message #":")]
(println "command" cmd "(" args ")")
(condp = cmd
"ping" (>! ws-ch "pong")
"Unknown command :/")) (keys @clients)
(recur))))))))
;;(close! ws-ch)))))

View File

@ -0,0 +1,35 @@
(ns sliva.socket
(:require [chord.client :refer [ws-ch]]
[cljs.core.async :refer [<! >! put! close!]]
[sliva.data :refer [vtic]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
;; Zacetek ;;
;;(aset js/document "onreadystatechange"
;; (fn []
;; (if (= (.-readyState js/document) "complete")
;; (do
;; (start-render)
;; (render)
;; (websocket-init)))))
(defn websocket-init []
(console.log "init webscoket")
(go (let [{:keys [ws-channel error]} (<! (ws-ch "ws://localhost:3449/ws"))]
(if-not error
(do
(reset! vtic ws-channel)
(>! ws-channel "Hello server")
;; (go-loop []
;; (let [{:keys [message]} (<! ws-channel)]
;; (console.log "got msg" message)
;; (socket-handler message)
;; (recur))))
)
(js/console.log "Fejl websocket: " (pr-str error))))))
(defn click-test []
(.addEventListener js/window "click" (fn []
(go []
(>! @vtic "ping")))))