summaryrefslogtreecommitdiffstats
path: root/bridge/api
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-12-07 23:48:44 +0100
committerWim <wim@42.be>2017-12-07 23:48:44 +0100
commitd30ae19e2a789f7bf5d430f9c7d065268d6a2957 (patch)
treedef175d6685aaec2d0927eeef4deb56b4b6a24b2 /bridge/api
parent5c919e6bffbf5dbe057623323effdbed2e82189e (diff)
downloadmatterbridge-msglm-d30ae19e2a789f7bf5d430f9c7d065268d6a2957.tar.gz
matterbridge-msglm-d30ae19e2a789f7bf5d430f9c7d065268d6a2957.tar.bz2
matterbridge-msglm-d30ae19e2a789f7bf5d430f9c7d065268d6a2957.zip
Add (simple, one listener) long-polling support (api). Closes #307
Diffstat (limited to 'bridge/api')
-rw-r--r--bridge/api/api.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/bridge/api/api.go b/bridge/api/api.go
index ab473afc..b422c8d9 100644
--- a/bridge/api/api.go
+++ b/bridge/api/api.go
@@ -1,6 +1,7 @@
package api
import (
+ "encoding/json"
"github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus"
"github.com/labstack/echo"
@@ -8,6 +9,7 @@ import (
"github.com/zfjagann/golang-ring"
"net/http"
"sync"
+ "time"
)
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/stream", b.handleStream)
e.POST("/api/message", b.handlePostMessage)
go func() {
flog.Fatal(e.Start(cfg.BindAddress))
@@ -103,3 +106,25 @@ func (b *Api) handleMessages(c echo.Context) error {
b.Messages = ring.Ring{}
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
+}