我正尝试读取一个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))))))