ws improvements
This commit is contained in:
parent
cf27863841
commit
80d8d10c15
4 changed files with 8 additions and 82 deletions
14
drone.go
14
drone.go
|
@ -135,17 +135,13 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
events := api.Group("/stream")
|
stream := api.Group("/stream")
|
||||||
{
|
{
|
||||||
events.GET("/user", server.GetEvents)
|
stream.Use(server.SetRepo())
|
||||||
|
stream.Use(server.SetPerm())
|
||||||
|
stream.GET("/:owner/:name", server.GetRepoEvents)
|
||||||
|
stream.GET("/:owner/:name/:build/:number", server.GetStream)
|
||||||
|
|
||||||
stream := events.Group("/logs")
|
|
||||||
{
|
|
||||||
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")
|
auth := r.Group("/authorize")
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
callback = _callback;
|
callback = _callback;
|
||||||
|
|
||||||
var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'),
|
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 = new WebSocket(route);
|
||||||
websocket.onmessage = function (event) {
|
websocket.onmessage = function (event) {
|
||||||
|
|
|
@ -87,7 +87,7 @@
|
||||||
callback = _callback;
|
callback = _callback;
|
||||||
|
|
||||||
var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'),
|
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 = new WebSocket(route);
|
||||||
websocket.onmessage = function (event) {
|
websocket.onmessage = function (event) {
|
||||||
|
|
72
server/ws.go
72
server/ws.go
|
@ -32,75 +32,6 @@ var upgrader = websocket.Upgrader{
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
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
|
// GetRepoEvents will upgrade the connection to a Websocket and will stream
|
||||||
// event updates to the browser.
|
// event updates to the browser.
|
||||||
func GetRepoEvents(c *gin.Context) {
|
func GetRepoEvents(c *gin.Context) {
|
||||||
|
@ -122,6 +53,7 @@ func GetRepoEvents(c *gin.Context) {
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
ws.Close()
|
ws.Close()
|
||||||
close(eventc)
|
close(eventc)
|
||||||
|
log.Debugf("closed websocket")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -139,7 +71,6 @@ func GetRepoEvents(c *gin.Context) {
|
||||||
err := ws.WriteMessage(websocket.PingMessage, []byte{})
|
err := ws.WriteMessage(websocket.PingMessage, []byte{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ws.Close()
|
ws.Close()
|
||||||
log.Debugf("closed websocket")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -147,7 +78,6 @@ func GetRepoEvents(c *gin.Context) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
readWebsocket(ws)
|
readWebsocket(ws)
|
||||||
log.Debugf("closed websocket")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetStream(c *gin.Context) {
|
func GetStream(c *gin.Context) {
|
||||||
|
|
Loading…
Reference in a new issue