Golang在我的流上超时了。

huangapple go评论112阅读模式
英文:

Golang is timing out on my stream

问题

我正在尝试编写一个 Go 服务器,该服务器从 HTTP 请求接收数据流。问题是,一旦接收到 {status: connected},连接就会超时。我该如何保持连接活动?

  1. func stream() {
  2. t := time.Now()
  3. a := t.Format("15:03")
  4. if a > string("08:30") && a < string("15:00") {
  5. color.Red("Streaming. . .")
  6. config := oauth1.NewConfig("HIDDEN", "HIDDEN")
  7. token := oauth1.NewToken("HIDDEN", "HIDDEN")
  8. httpClient := config.Client(oauth1.NoContext, token)
  9. path := "https://stream.tradeking.com/v1/market/quotes.json?symbols=aapl"
  10. resp, _ := httpClient.Get(path)
  11. fmt.Println(resp)
  12. body, _ := ioutil.ReadAll(resp.Body)
  13. fmt.Printf("Raw Response Body:\n%v\n", string(body))
  14. defer resp.Body.Close()
  15. } else {
  16. color.Red("%s \nMarkets are closed! Please, come back later.", t)
  17. }
  18. }

我在 main 函数中调用了这个函数。我还收到了看起来像是头部的内容:
&{200 OK 200 HTTP/1.1 1 1 map[Date:[Mon, 16 Jan 2017 17:45:36 GMT] X-Powered-By:[Express] Server:[cloudflare-ngi nx] Content-Type:[application/json] Connection:[keep-alive] Set-Cookie:[__cfduid=dcfcf86c79c56e659ce2b1bade3643c 981484588736; expires=Tue, 16-Jan-18 17:45:36 GMT; path=/; domain=.tradeking.com; HttpOnly] Vary:[Accept-Encodin g] Cf-Ray:[322361d0efd40938-DFW]] 0xc42032e0c0 -1 [chunked] false true map[] 0xc4200d21e0 0xc42040cbb0}

英文:

I am trying to write a go server that receives a stream of data from an http request. The problem is that it times out once it receives {status: connected} How do I keep the connection alive?

  1. func stream() {
  2. t := time.Now()
  3. a := t.Format(&quot;15:03&quot;)
  4. if a &gt; string(&quot;08:30&quot;) &amp;&amp; a &lt; string(&quot;15:00&quot;) {
  5. color.Red(&quot;Streaming. . .&quot;)
  6. config := oauth1.NewConfig(&quot;HIDDEN&quot;, &quot;HIDDEN&quot;)
  7. token := oauth1.NewToken(&quot;HIDDEN&quot;, &quot;HIDDEN&quot;)
  8. httpClient := config.Client(oauth1.NoContext, token)
  9. path := &quot;https://stream.tradeking.com/v1/market/quotes.json?symbols=aapl&quot;
  10. resp, _ := httpClient.Get(path)
  11. fmt.Println(resp)
  12. body, _ := ioutil.ReadAll(resp.Body)
  13. fmt.Printf(&quot;Raw Response Body:\n%v\n&quot;, string(body))
  14. defer resp.Body.Close()
  15. } else {
  16. color.Red(&quot;%s \nMarkets are closed! Please, come back later.&quot;, t)
  17. }
  18. }

I call this function in main. I also receive what looks like the header:
&amp;{200 OK 200 HTTP/1.1 1 1 map[Date:[Mon, 16 Jan 2017 17:45:36 GMT] X-Powered-By:[Express] Server:[cloudflare-ngi
nx] Content-Type:[application/json] Connection:[keep-alive] Set-Cookie:[__cfduid=dcfcf86c79c56e659ce2b1bade3643c
981484588736; expires=Tue, 16-Jan-18 17:45:36 GMT; path=/; domain=.tradeking.com; HttpOnly] Vary:[Accept-Encodin
g] Cf-Ray:[322361d0efd40938-DFW]] 0xc42032e0c0 -1 [chunked] false true map[] 0xc4200d21e0 0xc42040cbb0}

答案1

得分: 0

原来市场因为马丁·路德·金纪念日而关闭,因此服务器没有响应。

在一定的超时时间后,远程服务器会超时。

英文:

Turn out that the the market was closed for MLK Day and hence there was no response from the sever.

After a certain timeout, the remote server times out.

huangapple
  • 本文由 发表于 2017年1月17日 01:49:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/41682200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定