Skip to content

Instantly share code, notes, and snippets.

@mediocregopher
Created November 12, 2012 22:39
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 mediocregopher/4062522 to your computer and use it in GitHub Desktop.
Save mediocregopher/4062522 to your computer and use it in GitHub Desktop.
Holds a connection to socketspam open. To use: make-connection initializes the connection and sets up recovery. send-data sends whatever string you give it to socketspam.
(ns socketspam.pconn
(:use lamina.core aleph.tcp aleph.formats))
(def socketspam-host "localhost")
(def socketspam-port 9000)
;Create an endpoint and ground it so messages don't get queued up
;if nothing is listening
(def endpoint (channel))
(ground endpoint)
(defn send-data
"Sends the given data to the endpoint channel, which should hopefully
be siphoned into the tcp connection"
[data]
(enqueue endpoint data))
(declare make-connection)
(defn connection-established
"When a connection is established, forward everything going to endpoint into
it. Also, set up recovery handler"
[socket]
(println "Socket connection established")
(on-closed socket make-connection)
(siphon endpoint socket))
(defn connection-error
"On connection closed, wait a minute and try to recover. We use future
to keep the stack from growing (I think that will work)"
[error]
(println "Error connecting to socketspam: " error)
(Thread/sleep 1000)
(future (make-connection)))
(defn make-connection
"Initializes the connection to socketspam"
[]
(on-realized (tcp-client {:host socketspam-host :port socketspam-port})
connection-established
connection-error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment