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);
|
||||
};
|
||||
};
|
||||
|
|
22
server/ws.go
22
server/ws.go
|
@ -49,17 +49,21 @@ func GetRepoEvents(c *gin.Context) {
|
|||
}()
|
||||
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
event := <-eventc
|
||||
if event == nil {
|
||||
select {
|
||||
case event := <-eventc:
|
||||
if event == nil {
|
||||
log.Infof("nil event received")
|
||||
return false
|
||||
}
|
||||
if event.Kind == eventbus.EventRepo &&
|
||||
event.Name == repo.FullName {
|
||||
d := map[string]interface{}{}
|
||||
json.Unmarshal(event.Msg, &d)
|
||||
c.SSEvent("message", d)
|
||||
}
|
||||
case <-c.Writer.CloseNotify():
|
||||
return false
|
||||
}
|
||||
if event.Kind == eventbus.EventRepo &&
|
||||
event.Name == repo.FullName {
|
||||
d := map[string]interface{}{}
|
||||
json.Unmarshal(event.Msg, &d)
|
||||
c.SSEvent("message", d)
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue