Skip to content

Instantly share code, notes, and snippets.

@lepoetemaudit
Last active September 13, 2021 02:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepoetemaudit/9839a900bf9fd87afc83 to your computer and use it in GitHub Desktop.
Save lepoetemaudit/9839a900bf9fd87afc83 to your computer and use it in GitHub Desktop.
Dummy implementation a websocket server via a state machine in Clojure
(ns tokichat.core
(:use org.httpkit.server)
(:gen-class))
(defn handler [req]
(with-channel req channel
(on-close channel (fn [status]
(println "channel closed")))
(if-not (websocket? channel) (
(send! channel "Websocket required")
(close channel)))
(letfn [(start-fn [data]
(send! channel "That's interesting, tell me more about: ")
(send! channel data)
start-fn)]
(let [state-func (atom start-fn)]
(on-receive channel (fn [data]
(swap! state-func (@state-func data))
))))))
(defn -main []
(run-server handler {:port 8080})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment