1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-13 10:10:26 +00:00
janet/examples/tcpserver.janet

21 lines
556 B
Plaintext
Raw Normal View History

(defn handler
"Simple handler for connections."
[stream]
(defer (:close stream)
(def id (gensym))
(def b @"")
(print "Connection " id "!")
(while (:read stream 1024 b)
(repeat 10 (print "work for " id " ...") (ev/sleep 0.1))
(:write stream b)
(buffer/clear b))
(printf "Done %v!" id)))
2020-05-31 20:46:01 +00:00
# Run server.
(let [server (net/server "127.0.0.1" "8000")]
(print "Starting echo server on 127.0.0.1:8000")
(forever
2020-08-16 22:29:57 +00:00
(if-let [conn (:accept server)]
(ev/call handler conn)
(print "no new connections"))))