Golang HTTP服务器等待数据发送到客户端

Lee*_*ong 4 go

我正在创建类似于Twitter firehose / streaming API的流API。

据我所知,这是基于保持打开状态的HTTP连接的,当后端获取数据时,它会写入被阻止的HTTP连接。看来,我编写的任何代码都会在任何东西连接后立即关闭HTTP连接。

有没有办法保持这种开放?

func startHTTP(pathPrefix string) {
    log.Println("Starting HTTPS Server")
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        // Wait here until a write happens to w
        // Or we timeout, we can reset this timeout after each write
    })

    log.Print("HTTPS listening on :5556")
    log.Fatal(http.ListenAndServeTLS(":5556", pathPrefix+".crt", pathPrefix+".key", nil))
}
Run Code Online (Sandbox Code Playgroud)

ber*_*rkk 5

当您不希望立即但在某些事件之后将HTTP响应发送给客户端时,它称为long polling

这是一个长轮询的简单示例,它在客户端断开连接时取消请求:

package main

import (
    "context"
    "fmt"
    "net/http"
    "time"
)

func longOperation(ctx context.Context, ch chan<- string) {
    // Simulate long operation.
    // Change it to more than 10 seconds to get server timeout.
    select {
    case <-time.After(time.Second * 3):
        ch <- "Successful result."
    case <-ctx.Done():
        close(ch)
    }
}

func handler(w http.ResponseWriter, _ *http.Request) {
    notifier, ok := w.(http.CloseNotifier)
    if !ok {
        panic("Expected http.ResponseWriter to be an http.CloseNotifier")
    }

    ctx, cancel := context.WithCancel(context.Background())
    ch := make(chan string)
    go longOperation(ctx, ch)

    select {
    case result := <-ch:
        fmt.Fprint(w, result)
        cancel()
        return
    case <-time.After(time.Second * 10):
        fmt.Fprint(w, "Server is busy.")
    case <-notifier.CloseNotify():
        fmt.Println("Client has disconnected.")
    }
    cancel()
    <-ch
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe("localhost:8080", nil)
}
Run Code Online (Sandbox Code Playgroud)

网址:

  1. Golang:匿名结构和空结构
  2. 从Go服务器发送分块的HTTP响应
  3. 并发模式:上下文

要点:

  1. Golang长轮询示例
  2. 请求取消的Golang长轮询示例