2020-02-10 01:04:34 +00:00
|
|
|
(defn handler
|
|
|
|
"Simple handler for connections."
|
|
|
|
[stream]
|
2020-05-31 20:46:01 +00:00
|
|
|
(def id (gensym))
|
|
|
|
(def b @"")
|
|
|
|
(print "Connection " id "!")
|
|
|
|
(while (:read stream 1024 b)
|
2020-07-05 22:26:17 +00:00
|
|
|
(repeat 10 (print "work for " id " ...") (ev/sleep 1))
|
2020-05-31 20:46:01 +00:00
|
|
|
(:write stream b)
|
|
|
|
(buffer/clear b))
|
|
|
|
(printf "Done %v!" id))
|
2020-02-10 01:04:34 +00:00
|
|
|
|
2020-07-05 22:26:17 +00:00
|
|
|
(defn spawn-kid
|
|
|
|
"Run handler in a new fiber"
|
|
|
|
[conn]
|
|
|
|
(def f (fiber/new handler))
|
|
|
|
(ev/go f conn))
|
|
|
|
|
2020-05-28 21:51:11 +00:00
|
|
|
(print "Starting echo server on 127.0.0.1:8000")
|
2020-05-31 20:46:01 +00:00
|
|
|
|
|
|
|
(def server (net/server "127.0.0.1" "8000"))
|
|
|
|
|
|
|
|
# Run server.
|
|
|
|
(while true
|
|
|
|
(with [conn (:accept server)]
|
2020-07-05 22:26:17 +00:00
|
|
|
(spawn-kid conn)
|
|
|
|
(ev/sleep 0.1)))
|