1

I tried for few days, I am a little confused here. I am using clojure http-kit to make keepalive get request.

(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)
            ))

I am pretty sure that I made a persistent connection to target server, but nothing written to sample.data file. I tried as stream and as text.

I also tried ruby version the program create a persistent connection either, still nothing written.

So typically, the target will use webhook to notify my server new data is coming, but how to I get data from the persistent connection?

---EDIT---

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

Above is a working example using ruby, it uses a thread to read data from the connection. So I must miss something to get the data from clojure

crazy_phage
  • 550
  • 9
  • 28
  • I tried your code in repl and (except there is a parent missing) code works fine. The only problem is :as :stream will return ByteInputStream and when you try to spit it, you won't get text to file, but text description of the ByteInputSteam. May be you are trying to open a sample.data file in wrong directory? – JustAnotherCurious Sep 08 '15 at 08:40
  • I don't think the path matters, because I do not get the println either, and I tried http-kit :as :text, still nothing written to the file. I think I use the wrong way to get the data. – crazy_phage Sep 08 '15 at 13:38

0 Answers0