wip websockets

master
Jure Podgoršek 2018-08-05 20:34:45 +02:00
parent eade024c25
commit 79fc04f4d9
5 changed files with 90 additions and 14 deletions

View File

@ -0,0 +1,5 @@
1532652275720:(println "wat")
1532653287809:(java.util.UUID/randomUUID)
1532653323461:(console.log "wat")
1533493879172:(fig-status)
1533493885119:(reset-autobuild)

View File

@ -10,7 +10,10 @@
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/clojurescript "1.10.312"]
[org.clojure/core.async "0.4.474"]]
[org.clojure/core.async "0.4.474"]
[jarohen/chord "0.8.1"]
[ring "1.4.0"]
[compojure "1.5.0"]]
:plugins [[lein-figwheel "0.5.16"]
[lein-cljsbuild "1.1.7" :exclusions [[org.clojure/clojure]]]]
@ -64,7 +67,7 @@
;; doesn't work for you just run your own server :) (see lein-ring)
;; :ring-handler hello_world.server/handler
:ring-handler sliva.server/routes
;; To be able to open files in your editor from the heads up display
;; you will need to put a script on your path.
@ -85,8 +88,7 @@
;; :server-logfile "tmp/logs/figwheel-logfile.log"
;; to pipe all the output to the repl
;; :server-logfile false
}
:server-logfile false}
;; Setting up nREPL for Figwheel and ClojureScript dev
@ -94,12 +96,13 @@
;; https://github.com/bhauman/lein-figwheel/wiki/Using-the-Figwheel-REPL-within-NRepl
:profiles {:dev {:dependencies [[binaryage/devtools "0.9.10"]
[figwheel-sidecar "0.5.16"]
[cider/piggieback "0.3.6"]]
[cider/piggieback "0.3.6"]
[org.clojure/tools.nrepl "0.2.13"]]
;; need to add dev source path here to get user.clj loaded
:source-paths ["src" "dev"]
;; for CIDER
;; :plugins [[cider/cider-nrepl "0.12.0"]]
:repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}
:plugins [[cider/cider-nrepl "0.18.0-SNAPSHOT"]]
:repl-options {:nrepl-middleware [cider.piggieback/wrap-cljs-repl]}
;; need to add the compliled assets to the :clean-targets
:clean-targets ^{:protect false} ["resources/public/js/compiled"
:target-path]}})

View File

@ -1,4 +1,7 @@
(ns sliva.core)
(ns sliva.core
(:require [sliva.ctl :refer [websocket-init vtic]]
[cljs.core.async :refer [>!]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
(enable-console-print!)
@ -29,8 +32,6 @@
(def w-diff 0.5)
(def gostota-obj 2)
;; Inicializacija ;;
(def scena (THREE.Scene.))
@ -95,7 +96,7 @@
(.lookAt kamera scena.position))
(defn render []
(js/requestAnimationFrame render)
(.requestAnimationFrame js/window render)
(reset! stevec (inc @stevec))
@ -119,12 +120,15 @@
(aset js/document "onreadystatechange"
(fn []
(if (= (.-readyState js/document) "complete")
(start-render)
(render))))
(do
(start-render)
(render)
(websocket-init)))))
(defn on-js-reload []
(stop-render)
(start-render))
(start-render)
(websocket-init))
;; Hendlaj risajz
(defn on-window-resize[]
@ -134,3 +138,9 @@
(.updateProjectionMatrix kamera)
(.setSize izris sirina visina)))
(.addEventListener js/window "resize" on-window-resize false)
(.addEventListener js/window "click" (fn []
(console.log "kleek")
(console.log @vtic)
(go-loop []
(>! @vtic "WAT"))))

22
src/sliva/ctl.cljs 100644
View File

@ -0,0 +1,22 @@
(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,36 @@
(ns sliva.server
(:require [compojure.core :refer [defroutes GET]]
[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.core :refer [string]]))
(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)]
(let [[cmd & args] (split message #":")]
(condp = cmd
"ping" (>! ws-ch "pong")
"echo" (>! ws-ch (str "pong" args))
(println "Dunno what to do with command " cmd)))
(recur))))))))
;;(close! ws-ch)))))
(defroutes routes
(GET "/" [] (resource-response "index.html" {:root "public"}))
(GET "/hello" [] "wat")
(GET "/ws" [] socket-handler)
(resources "/"))
(def handler (-> #'routes
wrap-reload))