Skip to content

Instantly share code, notes, and snippets.

@duanebester
Last active September 26, 2021 21:07
Show Gist options
  • Save duanebester/980be9358af63167ee5cab7761ca4b5e to your computer and use it in GitHub Desktop.
Save duanebester/980be9358af63167ee5cab7761ca4b5e to your computer and use it in GitHub Desktop.
Getting and Closing a Buffer Socket
(defn connected? [^SocketChannel client]
(or (.isConnected client) (.isOpen client)))
(defn get-buffer-socket
[^Integer port ^String address]
(let [client (SocketChannel/open)
address (InetSocketAddress. address port)]
(.configureBlocking client true)
(.connect client address)
(init-buffer-socket client)))
(defn close-buffer-socket [{:keys [in out ^SocketChannel client]
:as this}]
(when (connected? client)
(async/close! in)
(async/close! out)
(.shutdownInput client)
(.shutdownOutput client)
(.close client)
(assoc this :client nil :in nil :out nil)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment