Skip to content

Commit

Permalink
Add SSE support
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Aug 2, 2014
1 parent c817295 commit cd797fe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
32 changes: 32 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/subtle"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
Expand Down Expand Up @@ -115,6 +116,7 @@ func (a *api) Start() {
a.Post(robotCommandRoute, a.executeRobotCommand)
a.Get("/api/robots/:robot/devices", a.robotDevices)
a.Get("/api/robots/:robot/devices/:device", a.robotDevice)
a.Get("/api/robots/:robot/devices/:device/events/:event", a.robotDeviceEvent)
a.Get("/api/robots/:robot/devices/:device/commands", a.robotDeviceCommands)
a.Get(deviceCommandRoute, a.executeDeviceCommand)
a.Post(deviceCommandRoute, a.executeDeviceCommand)
Expand Down Expand Up @@ -194,6 +196,36 @@ func (a *api) robotDevice(res http.ResponseWriter, req *http.Request) {
)
}

func (a *api) robotDeviceEvent(res http.ResponseWriter, req *http.Request) {
f, _ := res.(http.Flusher)
c, _ := res.(http.CloseNotifier)

closer := c.CloseNotify()
msg := make(chan string)

res.Header().Set("Content-Type", "text/event-stream")
res.Header().Set("Cache-Control", "no-cache")
res.Header().Set("Connection", "keep-alive")

gobot.On(a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).Event(req.URL.Query().Get(":event")),
func(data interface{}) {
d, _ := json.Marshal(data)
msg <- string(d)
})

for {
select {
case data := <-msg:
fmt.Fprintf(res, "data: %v\n\n", data)
f.Flush()
case <-closer:
log.Println("Closing connection")
return
}
}
}

func (a *api) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
a.writeJSON(
map[string]interface{}{"commands": a.gobot.Robot(req.URL.Query().Get(":robot")).
Expand Down
8 changes: 8 additions & 0 deletions examples/batty.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/api"
Expand All @@ -18,9 +19,16 @@ func main() {

loopback := gobot.NewLoopbackAdaptor("loopback")
ping := gobot.NewPingDriver(loopback, "ping")

work := func() {
gobot.Every(5*time.Second, func() {
fmt.Println(ping.Ping())
})
}
r := gobot.NewRobot("TestBot",
[]gobot.Connection{loopback},
[]gobot.Device{ping},
work,
)

r.AddCommand("hello", func(params map[string]interface{}) interface{} {
Expand Down
9 changes: 8 additions & 1 deletion test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,16 @@ func NewPingDriver(adaptor *loopbackAdaptor, name string) *pingDriver {
),
}

t.AddEvent("ping")

t.AddCommand("ping", func(params map[string]interface{}) interface{} {
return fmt.Sprintf("pong")
return t.Ping()
})

return t
}

func (t *pingDriver) Ping() string {
Publish(t.Event("ping"), "ping")
return "pong"
}

0 comments on commit cd797fe

Please sign in to comment.