Golang Context.Stream Examples

Golang Context.Stream - 8 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.Stream extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func Generate8bit(c *gin.Context) {
	name := c.Param("name")
	gender := c.Param("gender")

	switch {
	case gender == "m":
		gender = "male"
	case gender == "f":
		gender = "female"
	case gender == "male" || gender == "female":
		//do nothing
	default:
		c.AbortWithError(http.StatusInternalServerError, errors.New("Invalid parameters"))
		return
	}

	log.Println(name)
	InitAssets()
	img := GenerateIdenticon8bits(gender, []byte(name))
	c.Header("Content-Type", "image/png")
	c.Stream(func(w io.Writer) bool {
		png.Encode(w, img)
		return false
	})
}
Example #2
0
// GetRepoEvents will upgrade the connection to a Websocket and will stream
// event updates to the browser.
func GetRepoEvents(c *gin.Context) {
	engine_ := context.Engine(c)
	repo := session.Repo(c)
	c.Writer.Header().Set("Content-Type", "text/event-stream")

	eventc := make(chan *engine.Event, 1)
	engine_.Subscribe(eventc)
	defer func() {
		engine_.Unsubscribe(eventc)
		close(eventc)
		log.Infof("closed event stream")
	}()

	c.Stream(func(w io.Writer) bool {
		select {
		case event := <-eventc:
			if event == nil {
				log.Infof("nil event received")
				return false
			}
			if event.Name == repo.FullName {
				log.Debugf("received message %s", event.Name)
				sse.Encode(w, sse.Event{
					Event: "message",
					Data:  string(event.Msg),
				})
			}
		case <-c.Writer.CloseNotify():
			return false
		}
		return true
	})
}
Example #3
0
func (app *appContext) timeStream(c *gin.Context) {
	recv := app.timeGroup.Join()
	defer recv.Close()
	c.Stream(func(w io.Writer) bool {
		c.SSEvent("message", recv.Recv().(string))
		return true
	})
}
Example #4
0
func stream(c *gin.Context) {
	roomid := c.Param("roomid")
	listener := openListener(roomid)
	defer closeListener(roomid, listener)

	c.Stream(func(w io.Writer) bool {
		c.SSEvent("message", <-listener)
		return true
	})
}
Example #5
0
func GenerateGitHub(c *gin.Context, name string, width, margin int) {
	if width < 32 || width-margin < 10 {
		c.AbortWithError(http.StatusInternalServerError, errors.New("Invalid parameters"))
		return
	}

	img := GenerateIdenticon([]byte(name), width, margin)
	c.Header("Content-Type", "image/png")
	c.Stream(func(w io.Writer) bool {
		png.Encode(w, img)
		return false
	})
}
Example #6
0
func stream(ctx *gin.Context) {

	ticker := time.NewTicker(500 * time.Millisecond)
	defer func() {
		ticker.Stop()
	}()

	ctx.Stream(func(w io.Writer) bool {
		select {
		case tm := <-ticker.C:

			ctx.SSEvent("", tm)

		}
		return true
	})
}