Restrukturiranje projekta, socket fix
parent
d421a03cc1
commit
29da6fa0d3
16
project.clj
16
project.clj
|
@ -13,7 +13,9 @@
|
||||||
[org.clojure/core.async "0.4.474"]
|
[org.clojure/core.async "0.4.474"]
|
||||||
[jarohen/chord "0.8.1"]
|
[jarohen/chord "0.8.1"]
|
||||||
[ring "1.7.0-RC1"]
|
[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"]
|
:plugins [[lein-figwheel "0.5.16"]
|
||||||
[lein-cljsbuild "1.1.7" :exclusions [[org.clojure/clojure]]]]
|
[lein-cljsbuild "1.1.7" :exclusions [[org.clojure/clojure]]]]
|
||||||
|
@ -24,15 +26,7 @@
|
||||||
[{:id "dev"
|
[{:id "dev"
|
||||||
:source-paths ["src"]
|
:source-paths ["src"]
|
||||||
|
|
||||||
;; The presence of a :figwheel configuration here
|
:figwheel {:on-jsload "sliva.core/init-app"}
|
||||||
;; 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"]}
|
|
||||||
|
|
||||||
:compiler {:main sliva.core
|
:compiler {:main sliva.core
|
||||||
:asset-path "js/compiled/out"
|
:asset-path "js/compiled/out"
|
||||||
|
@ -67,7 +61,7 @@
|
||||||
|
|
||||||
;; doesn't work for you just run your own server :) (see lein-ring)
|
;; 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
|
;; To be able to open files in your editor from the heads up display
|
||||||
;; you will need to put a script on your path.
|
;; you will need to put a script on your path.
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
</head>
|
</head>
|
||||||
<body style="margin: 0">
|
<body style="margin: 0">
|
||||||
<div id="container"></div>
|
<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>
|
<script src="js/compiled/sliva.js" type="text/javascript"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,144 +1,32 @@
|
||||||
(ns sliva.core
|
(ns sliva.core
|
||||||
(:require [sliva.ctl :refer [websocket-init vtic]]
|
(:require
|
||||||
[cljs.core.async :refer [>!]])
|
[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]]))
|
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
|
||||||
|
|
||||||
(enable-console-print!)
|
(enable-console-print!)
|
||||||
|
|
||||||
(console.log "Hello, Sky!")
|
(console.log "Hello, Sky!")
|
||||||
|
|
||||||
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
|
;; Page switching
|
||||||
;; ☭☭☭☭ Parametri razni ☭☭☭☭ ;;
|
(defmulti current-page #(@appstate :page))
|
||||||
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
|
(defmethod current-page :hub [] hub)
|
||||||
|
|
||||||
(def odmik-kamere 100)
|
;;(defn on-js-reload []
|
||||||
(def rotacija-kamere 1)
|
;; (stop-render)
|
||||||
(def FOV 140)
|
;; (start-render)
|
||||||
(def lik-sirina 2)
|
;; (websocket-init))
|
||||||
(def obj-limit 1000)
|
|
||||||
|
|
||||||
(def objekti (atom []))
|
;; On figwheel reload do this (reload routes, render currently active page)
|
||||||
(def stevec (atom 0))
|
(defn init-app []
|
||||||
|
(app-routes)
|
||||||
|
(reagent/render [current-page] (.getElementById js/document "container")))
|
||||||
|
|
||||||
(def rotacija-x 0.006)
|
;; Vstopna tocka
|
||||||
(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 ;;
|
|
||||||
(aset js/document "onreadystatechange"
|
(aset js/document "onreadystatechange"
|
||||||
(fn []
|
(fn []
|
||||||
(if (= (.-readyState js/document) "complete")
|
(if (= (.-readyState js/document) "complete")
|
||||||
(do
|
(init-app))))
|
||||||
(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"))))
|
|
||||||
|
|
|
@ -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))))))
|
|
|
@ -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)))
|
|
@ -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)
|
|
@ -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)))]]])
|
|
@ -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!))
|
|
@ -3,32 +3,11 @@
|
||||||
[compojure.route :refer [resources]]
|
[compojure.route :refer [resources]]
|
||||||
[ring.util.response :refer [resource-response]]
|
[ring.util.response :refer [resource-response]]
|
||||||
[ring.middleware.reload :refer [wrap-reload]]
|
[ring.middleware.reload :refer [wrap-reload]]
|
||||||
[chord.http-kit :refer [with-channel]]
|
[sliva.server.handlers :refer [socket-handler]]))
|
||||||
[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)))))
|
|
||||||
|
|
||||||
|
;; wat
|
||||||
(defroutes routes
|
(defroutes routes
|
||||||
(GET "/" [] (resource-response "index.html" {:root "public"}))
|
;;(GET "/" [] (resource-response "index.html" {:root "public"}))
|
||||||
(GET "/hello" [] "wat")
|
(GET "/hello" [] "wat")
|
||||||
(GET "/ws" [] socket-handler)
|
(GET "/ws" [] socket-handler)
|
||||||
(resources "/"))
|
(resources "/"))
|
||||||
|
|
|
@ -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)))))
|
|
@ -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")))))
|
Loading…
Reference in New Issue