github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/web/examples/streaming.go (about)

     1  // Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build ingore
     6  
     7  package main
     8  
     9  import (
    10  	"net/http"
    11  	"strconv"
    12  	"time"
    13  
    14  	"github.com/chai2010/gopkg/web"
    15  )
    16  
    17  func hello(ctx *web.Context, num string) {
    18  	flusher, _ := ctx.ResponseWriter.(http.Flusher)
    19  	flusher.Flush()
    20  	n, _ := strconv.ParseInt(num, 10, 64)
    21  	for i := int64(0); i < n; i++ {
    22  		ctx.WriteString("<br>hello world</br>")
    23  		flusher.Flush()
    24  		time.Sleep(1e9)
    25  	}
    26  }
    27  
    28  func main() {
    29  	web.Get("/([0-9]+)", hello)
    30  	web.Run("0.0.0.0:9999")
    31  }