Skip to content

Instantly share code, notes, and snippets.

@devn
Created April 6, 2010 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devn/358134 to your computer and use it in GitHub Desktop.
Save devn/358134 to your computer and use it in GitHub Desktop.
(defn connect [server]
(let [socket (Socket. (:name server) (:port server))
in (BufferedReader. (InputStreamReader. (.getInputStream socket)))
out (PrintWriter. (.getOutputStream socket))
conn (ref {:in in :out out})]
(doto (Thread. #(conn-handler conn)) (.start))
conn))
(defn write [conn msg]
(doto (:out @conn)
(.println (str msg "\r"))
(.flush)))
(defn conn-handler [conn]
(while
(nil? (:exit @conn))
(let [msg (.readLine (:in @conn))]
(println msg)
(cond
(re-find #"^ERROR :Closing Link:" msg)
(dosync (alter conn merge {:exit true}))
(re-find #"^PING" msg)
(write conn (str "PONG " (re-find #":.*" msg)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment