prevent hanging event source connections
This commit is contained in:
parent
b3951043a0
commit
2e9786c68e
2 changed files with 18 additions and 10 deletions
|
@ -88,12 +88,16 @@
|
|||
|
||||
events = new EventSource("/api/stream/" + repo + "?access_token=" + token, { withCredentials: true });
|
||||
events.onmessage = function (event) {
|
||||
console.log(event);
|
||||
if (callback !== undefined) {
|
||||
callback(angular.fromJson(event.data));
|
||||
}
|
||||
};
|
||||
events.onerror = function (event) {
|
||||
callback = undefined;
|
||||
if (events !== undefined) {
|
||||
events.close();
|
||||
events = undefined;
|
||||
}
|
||||
console.log('user event stream closed due to error.', event);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -49,8 +49,10 @@ func GetRepoEvents(c *gin.Context) {
|
|||
}()
|
||||
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
event := <-eventc
|
||||
select {
|
||||
case event := <-eventc:
|
||||
if event == nil {
|
||||
log.Infof("nil event received")
|
||||
return false
|
||||
}
|
||||
if event.Kind == eventbus.EventRepo &&
|
||||
|
@ -59,7 +61,9 @@ func GetRepoEvents(c *gin.Context) {
|
|||
json.Unmarshal(event.Msg, &d)
|
||||
c.SSEvent("message", d)
|
||||
}
|
||||
|
||||
case <-c.Writer.CloseNotify():
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue