1

我尝试了几天,我在这里有点困惑。我正在使用 clojure http-kit 发出 keepalive 获取请求。

(ns weibo-collector.weibo
    (:require [org.httpkit.client :as http]
              [clojure.java.io :as io]))
(def sub-url "http://c.api.weibo.com/datapush/status?subid=10542")

(defn spit-to-file [content]
  (spit "sample.data" content :append true))

@(http/get sub-url {:as :stream :keepalive 3000000}
          (fn [{:keys [status headers body error opts]}]
            (spit-to-file body)
            ))

我很确定我与目标服务器建立了持久连接,但没有写入 sample.data 文件。我尝试作为流和文本。

我也试过 ruby​​ 版本的程序创建一个持久连接,仍然没有写。

所以通常情况下,目标会使用 webhook 来通知我的服务器有新数据到来,但是我如何从持久连接中获取数据呢?

- -编辑 - -

require 'awesome_print'
url = "http://c.api.weibo.com/datapush/status?subid=10542"

require "httpclient"

c = HTTPClient.new

conn = c.get_async(url)

Thread.new do
  res = conn.pop
  while true
  text = ""
  while ch = res.content.read(1)
    text = text+ch
    break if text.end_with? "\r\n"
  end
  ap text
 end
end

while true
end

上面是一个使用 ruby​​ 的工作示例,它使用线程从连接中读取数据。所以我必须错过一些东西才能从 clojure 获取数据

4

0 回答 0