Last active
September 13, 2021 02:17
-
-
Save lepoetemaudit/9839a900bf9fd87afc83 to your computer and use it in GitHub Desktop.
Dummy implementation a websocket server via a state machine in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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