prevent hanging event source connections

This commit is contained in:
Brad Rydzewski 2015-05-16 20:35:41 -07:00
parent b3951043a0
commit 2e9786c68e
2 changed files with 18 additions and 10 deletions

View file

@ -88,12 +88,16 @@
events = new EventSource("/api/stream/" + repo + "?access_token=" + token, { withCredentials: true }); events = new EventSource("/api/stream/" + repo + "?access_token=" + token, { withCredentials: true });
events.onmessage = function (event) { events.onmessage = function (event) {
console.log(event);
if (callback !== undefined) { if (callback !== undefined) {
callback(angular.fromJson(event.data)); callback(angular.fromJson(event.data));
} }
}; };
events.onerror = function (event) { events.onerror = function (event) {
callback = undefined;
if (events !== undefined) {
events.close();
events = undefined;
}
console.log('user event stream closed due to error.', event); console.log('user event stream closed due to error.', event);
}; };
}; };

View file

@ -49,17 +49,21 @@ func GetRepoEvents(c *gin.Context) {
}() }()
c.Stream(func(w io.Writer) bool { c.Stream(func(w io.Writer) bool {
event := <-eventc select {
if event == nil { 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 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 return true
}) })
} }