Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 从http持久连接获取数据_Ruby_Clojure_Http Kit - Fatal编程技术网

Ruby 从http持久连接获取数据

Ruby 从http持久连接获取数据,ruby,clojure,http-kit,Ruby,Clojure,Http Kit,我试了几天,我有点困惑。 我正在使用clojure http工具包发出keepalive get请求 (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 [

我试了几天,我有点困惑。 我正在使用clojure http工具包发出keepalive get请求

(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通知我的服务器新数据即将到来,但是如何从持久连接获取数据呢

---编辑---


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

我在repl中尝试了您的代码,并且(除了缺少一个父级)代码工作正常。唯一的问题是:as:stream将返回ByteInputStream,当您尝试吐出它时,您将不会得到文件中的文本,而是ByteInputStream的文本描述。可能是您试图在错误的目录中打开sample.data文件?我认为路径无关紧要,因为我也没有得到println,并且我尝试了http kit:as:text,仍然没有写入文件。我想我使用了错误的方法来获取数据。我在repl中尝试了你的代码,并且(除了缺少一个父级)代码工作正常。唯一的问题是:as:stream将返回ByteInputStream,当您尝试吐出它时,您将不会得到文件中的文本,而是ByteInputStream的文本描述。可能是您试图在错误的目录中打开sample.data文件?我认为路径无关紧要,因为我也没有得到println,并且我尝试了http kit:as:text,仍然没有写入文件。我想我用了错误的方法来获取数据。
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