new: implemented optional websocket for /api/events

This commit is contained in:
evilsocket 2018-03-06 13:54:37 +01:00
commit 5ad1a17118
4 changed files with 66 additions and 36 deletions

View file

@ -57,7 +57,7 @@ func NewEventPool(debug bool, silent bool) *EventPool {
func (p *EventPool) Listen() <-chan Event {
p.Lock()
defer p.Unlock()
l := make(chan Event)
l := make(chan Event, 1)
p.listeners = append(p.listeners, l)
return l
}
@ -77,12 +77,16 @@ func (p *EventPool) SetDebug(d bool) {
func (p *EventPool) Add(tag string, data interface{}) {
p.Lock()
defer p.Unlock()
e := NewEvent(tag, data)
p.events = append([]Event{e}, p.events...)
// broadcast the event to every listener
for _, l := range p.listeners {
l <- e
select {
case l <- e:
default:
}
}
}