ws improvements

This commit is contained in:
Brad Rydzewski 2015-05-05 20:26:57 -07:00
parent cf27863841
commit 80d8d10c15
4 changed files with 8 additions and 82 deletions

View file

@ -135,17 +135,13 @@ func main() {
}
}
events := api.Group("/stream")
{
events.GET("/user", server.GetEvents)
stream := events.Group("/logs")
stream := api.Group("/stream")
{
stream.Use(server.SetRepo())
stream.Use(server.SetPerm())
stream.GET("/:owner/:name", server.GetRepoEvents)
stream.GET("/:owner/:name/:build/:number", server.GetStream)
}
}
auth := r.Group("/authorize")

View file

@ -27,7 +27,7 @@
callback = _callback;
var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'),
route = [proto, "://", $window.location.host, '/api/stream/logs/', repoName, '/', number, '/', step, '?access_token=', token].join('');
route = [proto, "://", $window.location.host, '/api/stream/', repoName, '/', number, '/', step, '?access_token=', token].join('');
websocket = new WebSocket(route);
websocket.onmessage = function (event) {

View file

@ -87,7 +87,7 @@
callback = _callback;
var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'),
route = [proto, "://", $window.location.host, '/api/stream/logs/'+ repo +'?access_token=', token].join('');
route = [proto, "://", $window.location.host, '/api/stream/'+ repo +'?access_token=', token].join('');
websocket = new WebSocket(route);
websocket.onmessage = function (event) {

View file

@ -32,75 +32,6 @@ var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
// GetEvents will upgrade the connection to a Websocket and will stream
// event updates to the browser.
func GetEvents(c *gin.Context) {
// bus := ToBus(c)
// user := ToUser(c)
// remote := ToRemote(c)
//
// // TODO (bradrydzewski) revisit this approach at some point.
// //
// // instead of constantly checking for remote permissions, we will
// // cache them for the lifecycle of this websocket. The pro here is
// // that we are making way less external calls (good). The con is that
// // if a ton of developers conntect to websockets for long periods of
// // time with heavy build traffic (not super likely, but possible) this
// // caching strategy could take up a lot of memory.
// perms_ := map[string]*common.Perm{}
//
// // upgrade the websocket
// ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
// if err != nil {
// c.Fail(400, err)
// return
// }
//
// ticker := time.NewTicker(pingPeriod)
// eventc := make(chan *eventbus.Event, 1)
// bus.Subscribe(eventc)
// defer func() {
// bus.Unsubscribe(eventc)
// ticker.Stop()
// ws.Close()
// close(eventc)
// }()
//
// go func() {
// for {
// select {
// case event := <-eventc:
// if event == nil {
// return // why would this ever happen?
// }
// perm, ok := perms_[event.Repo.FullName]
// if !ok {
// perm = perms(remote, user, event.Repo)
// perms_[event.Repo.FullName] = perm
// }
//
// if perm != nil && perm.Pull {
// err := ws.WriteJSON(event)
// if err != nil {
// log.Errorln(err, event)
// }
// }
// case <-ticker.C:
// ws.SetWriteDeadline(time.Now().Add(writeWait))
// err := ws.WriteMessage(websocket.PingMessage, []byte{})
// if err != nil {
// ws.Close()
// log.Debugf("closed websocket")
// return
// }
// }
// }
// }()
//
// readWebsocket(ws)
// log.Debugf("closed websocket")
}
// GetRepoEvents will upgrade the connection to a Websocket and will stream
// event updates to the browser.
func GetRepoEvents(c *gin.Context) {
@ -122,6 +53,7 @@ func GetRepoEvents(c *gin.Context) {
ticker.Stop()
ws.Close()
close(eventc)
log.Debugf("closed websocket")
}()
go func() {
@ -139,7 +71,6 @@ func GetRepoEvents(c *gin.Context) {
err := ws.WriteMessage(websocket.PingMessage, []byte{})
if err != nil {
ws.Close()
log.Debugf("closed websocket")
return
}
}
@ -147,7 +78,6 @@ func GetRepoEvents(c *gin.Context) {
}()
readWebsocket(ws)
log.Debugf("closed websocket")
}
func GetStream(c *gin.Context) {