mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-25 22:31:35 +00:00
Add (simple, one listener) long-polling support (api). Closes #307
This commit is contained in:
parent
5c919e6bff
commit
d30ae19e2a
@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
@ -8,6 +9,7 @@ import (
|
|||||||
"github.com/zfjagann/golang-ring"
|
"github.com/zfjagann/golang-ring"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Api struct {
|
type Api struct {
|
||||||
@ -47,6 +49,7 @@ func New(cfg config.Protocol, account string, c chan config.Message) *Api {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
e.GET("/api/messages", b.handleMessages)
|
e.GET("/api/messages", b.handleMessages)
|
||||||
|
e.GET("/api/stream", b.handleStream)
|
||||||
e.POST("/api/message", b.handlePostMessage)
|
e.POST("/api/message", b.handlePostMessage)
|
||||||
go func() {
|
go func() {
|
||||||
flog.Fatal(e.Start(cfg.BindAddress))
|
flog.Fatal(e.Start(cfg.BindAddress))
|
||||||
@ -103,3 +106,25 @@ func (b *Api) handleMessages(c echo.Context) error {
|
|||||||
b.Messages = ring.Ring{}
|
b.Messages = ring.Ring{}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Api) handleStream(c echo.Context) error {
|
||||||
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||||
|
c.Response().WriteHeader(http.StatusOK)
|
||||||
|
closeNotifier := c.Response().CloseNotify()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-closeNotifier:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
msg := b.Messages.Dequeue()
|
||||||
|
if msg != nil {
|
||||||
|
if err := json.NewEncoder(c.Response()).Encode(msg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.Response().Flush()
|
||||||
|
}
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user