Skip to content

Instantly share code, notes, and snippets.

@mfikes
Last active June 3, 2017 18:14
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 mfikes/e44cf07e8217db2112facf90b550ab74 to your computer and use it in GitHub Desktop.
Save mfikes/e44cf07e8217db2112facf90b550ab74 to your computer and use it in GitHub Desktop.
Planck Sockets and Unrepl

Planck master contains alpha code to listen on a socket. This is currently exposed via an planck.socket.alpha namespace.

To start listening, call listen. It takes 2 arguments, the port and a function of a single socket argument that is called when a socket connection is accepted.

This "accept" function should return a function that can handle data that arrives on the socket. This "data handler" function takes two arguents, a socket and the data that arrived. I'm thinking that nil data can indicate that the socket has been closed by the remote end (but this hasn't been implemented).

To write on a socket, call write, passing the socket and the string to write.

In the above functions, the socket is just data (it is the file descriptor integer for the socket).

Putting the above together, here is an example of starting an echo server in Planck:

(listen 8888 
  (fn [socket] 
    (fn [socket data]
      (when data
        (write socket data)))))

With these rudimentary building blocks, the Unrepl code can be made to work.

$ planck -qc src
cljs.user=> (require '[net.cgrand.planck.socket-repl :as r])
nil
cljs.user=> (r/start-server 7777)
nil
$ nc localhost 7777
cljs.user=>(+ 1 2)
3
cljs.user=>(def a 3)
#'cljs.user/a
cljs.user=>a
3
cljs.user=>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment