本文整理匯總了Golang中github.com/gin-gonic/gin.Context.Stream方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Stream方法的具體用法?Golang Context.Stream怎麽用?Golang Context.Stream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.Stream方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Generate8bit
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
})
}
示例2: timeStream
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
})
}
示例3: stream
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
})
}
示例4: GenerateGitHub
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
})
}
示例5: stream
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
})
}
示例6: GetRepoEvents
// GetRepoEvents will upgrade the connection to a Websocket and will stream
// event updates to the browser.
func GetRepoEvents(c *gin.Context) {
repo := session.Repo(c)
c.Writer.Header().Set("Content-Type", "text/event-stream")
eventc := make(chan *bus.Event, 1)
bus.Subscribe(c, eventc)
defer func() {
bus.Unsubscribe(c, 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
}
// TODO(bradrydzewski) This is a super hacky workaround until we improve
// the actual bus. Having a per-call database event is just plain stupid.
if event.Repo.FullName == repo.FullName {
var payload = struct {
model.Build
Jobs []*model.Job `json:"jobs"`
}{}
payload.Build = event.Build
payload.Jobs, _ = store.GetJobList(c, &event.Build)
data, _ := json.Marshal(&payload)
sse.Encode(w, sse.Event{
Event: "message",
Data: string(data),
})
}
case <-c.Writer.CloseNotify():
return false
}
return true
})
}
示例7: streamRoom
func streamRoom(c *gin.Context) {
roomid := c.Param("roomid")
listener := openListener(roomid)
ticker := time.NewTicker(1 * time.Second)
users.Add("connected", 1)
defer func() {
closeListener(roomid, listener)
ticker.Stop()
users.Add("disconnected", 1)
}()
c.Stream(func(w io.Writer) bool {
select {
case msg := <-listener:
messages.Add("outbound", 1)
c.SSEvent("message", msg)
case <-ticker.C:
c.SSEvent("stats", Stats())
}
return true
})
}
示例8: GetRepoEvents
// 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
})
}