This commit is contained in:
Michiel Borkent 2020-04-05 11:54:10 +02:00
parent 3242e0dab3
commit 847f872df8

View file

@ -714,19 +714,22 @@ This can be useful for talking to Docker:
Babashka comes with the [nrepl/bencode](https://github.com/nrepl/bencode) Babashka comes with the [nrepl/bencode](https://github.com/nrepl/bencode)
library which allows you to read and write bencode messages to a socket. A library which allows you to read and write bencode messages to a socket. A
simple example which gets the Clojure version from an nREPL server started with simple example which evaluates a Clojure expression on an nREPL server started
`lein repl`: with `lein repl`:
``` clojure ``` clojure
(ns nrepl-client (ns nrepl-client
(:require [bencode.core :as b])) (:require [bencode.core :as b]))
(let [s (java.net.Socket. "localhost" 65274) (defn nrepl-eval [port expr]
out (.getOutputStream s) (let [s (java.net.Socket. "localhost" port)
in (java.io.PushbackInputStream. (.getInputStream s)) out (.getOutputStream s)
_ (b/write-bencode out {"op" "describe"}) in (java.io.PushbackInputStream. (.getInputStream s))
clojure-version (get-in (b/read-bencode in) ["versions" "clojure" "version-string"])] _ (b/write-bencode out {"op" "eval" "code" (pr-str expr)})
(String. clojure-version "UTF-8")) ;;=> "1.10.0" value (get (b/read-bencode in) "value")]
(read-string value)))
(nrepl-eval 65274 '(+ 1 2 3)) ;;=> 6
``` ```