我正在尝试读取一个json
HTTP流并无限期地使用其中的json
元素。
我写出了下面的代码,但关闭了流,并且只返回了30个结果 - 我该如何无限期地使用HTTP流?
感谢你的帮助!
(ns core
(:require [clj-http.client :as http]
[cheshire.core :as json]
[clojure.java.io :as io]
[clojure.core.async :as async]))
(def gh-url "https://api.github.com/events")
(def chan (async/chan 100))
(async/go
(loop [r (async/<! chan)]
(when (not-empty r) (println (:type r)))
(recur (async/<! chan))))
(defn read-gh-stream [url]
(with-open [stream (-> url (http/get {:as :stream}) :body)]
(let [lines (-> stream io/reader (json/parse-stream true))]
(doseq [l lines]
(async/go
(async/>! chan l))))))