http goサーバーからの応答
は、このシナリオで、転送エンコードセットでチャンクされたhttp応答を送信するgo httpサーバーを作成することを目指しています。サーバーは、1秒間隔でチャンクを記述し、クライアントがオンデマンドでそれらを受信できるようにします。ただし、現在の実装は課題に直面しています。提供されたサーバーコードは次のとおりです。 w.header()。set( "Connection"、 "Keep-Alive") w.header()。set( "転送エンコード"、「チャンク ")) w.header()。set( "x-content-type-options"、 "nosniff") ティッカー:= time.newticker(time.second) go func(){ t:= range thicker.c { io.writestring(w、 "chunk") fmt.println( "tick at"、t) } }() time.sleep(time.second * 5) Ticker.Stop() fmt.println( "finish:content-length:0 here"を返す必要があります ") w.header()。set( "content-length"、 "0") }
verification
func HandlePost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Connection", "Keep-Alive") w.Header().Set("Transfer-Encoding", "chunked") w.Header().Set("X-Content-Type-Options", "nosniff") ticker := time.NewTicker(time.Second) go func() { for t := range ticker.C { io.WriteString(w, "Chunk") fmt.Println("Tick at", t) } }() time.Sleep(time.Second * 5) ticker.Stop() fmt.Println("Finished: should return Content-Length: 0 here") w.Header().Set("Content-Length", "0") }
$ telnet localhost 8080 ... HTTP/1.1 200 OK 日付: ... コンテンツタイプ:テキスト/プレーン; charset = utf-8 転送エンコード:チャンク 9 チャンク#1 9 チャンク#2 ... 各チャンクは、サーバーがそれらを送信すると徐々に受信されます。
免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。
Copyright© 2022 湘ICP备2022001581号-3