Warm tip: This article is reproduced from stackoverflow.com, please click
clojure http java response sockets

How can I get clojure to write a response to a socket?

发布于 2020-04-23 11:22:32

I've made a socket which will accept any get/post requests on a port.

And for now based on the uri it prints out different strings to the repl.

/ will just say "hello"

/whatever says "hello, whatever"

But now I need to send a response to the browser, this is what I have, but I'm not getting a response in the browser:

(def output (.getOutputStream accepted-socket))

(map (fn[char] (.write @output (int char)))

"HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: 44\nConnection: close\r\n\r\n

<html><body><h1 Please work!/></body></html>")
Questioner
Carl Helberg
Viewed
45
bfabry 2020-02-08 08:12
user=> (import 'java.net.Socket)
java.net.Socket
user=> (def s (Socket. "localhost" 7777))
#'user/s
user=> (with-open [o (io/writer s)]
(.write o "hello, world"))
nil