making all references to EventBusListeners pointers
adding a keepalive event type.
This commit is contained in:
parent
7078f784c4
commit
ce8efdb753
|
@ -29,13 +29,23 @@ func GetEventBusServer(logger logrus.FieldLogger) *EventBus {
|
||||||
singletonEventBusInstance = &EventBus{
|
singletonEventBusInstance = &EventBus{
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
Message: make(chan EventBusMessage),
|
Message: make(chan EventBusMessage),
|
||||||
NewListener: make(chan EventBusListener),
|
NewListener: make(chan *EventBusListener),
|
||||||
ClosedListener: make(chan EventBusListener),
|
ClosedListener: make(chan *EventBusListener),
|
||||||
TotalRoomListeners: make(map[string][]EventBusListener),
|
TotalRoomListeners: make(map[string][]*EventBusListener),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start processing requests
|
// Start processing requests
|
||||||
go singletonEventBusInstance.listen()
|
go singletonEventBusInstance.listen()
|
||||||
|
|
||||||
|
//background keep-alive for testing
|
||||||
|
//go func() {
|
||||||
|
// for {
|
||||||
|
// time.Sleep(time.Second * 10)
|
||||||
|
// // Send current time
|
||||||
|
// singletonEventBusInstance.PublishMessage(models.NewEventKeepAlive("keep-alive"))
|
||||||
|
// }
|
||||||
|
//}()
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Single instance already created.")
|
fmt.Println("Single instance already created.")
|
||||||
}
|
}
|
||||||
|
@ -55,13 +65,13 @@ type EventBus struct {
|
||||||
Message chan EventBusMessage
|
Message chan EventBusMessage
|
||||||
|
|
||||||
// New client connections
|
// New client connections
|
||||||
NewListener chan EventBusListener
|
NewListener chan *EventBusListener
|
||||||
|
|
||||||
// Closed client connections
|
// Closed client connections
|
||||||
ClosedListener chan EventBusListener
|
ClosedListener chan *EventBusListener
|
||||||
|
|
||||||
// Total client connections
|
// Total client connections
|
||||||
TotalRoomListeners map[string][]EventBusListener
|
TotalRoomListeners map[string][]*EventBusListener
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventBusListener struct {
|
type EventBusListener struct {
|
||||||
|
@ -84,7 +94,7 @@ func (bus *EventBus) listen() {
|
||||||
case listener := <-bus.NewListener:
|
case listener := <-bus.NewListener:
|
||||||
//check if this userId room already exists, or create it
|
//check if this userId room already exists, or create it
|
||||||
if _, exists := bus.TotalRoomListeners[listener.UserID]; !exists {
|
if _, exists := bus.TotalRoomListeners[listener.UserID]; !exists {
|
||||||
bus.TotalRoomListeners[listener.UserID] = []EventBusListener{}
|
bus.TotalRoomListeners[listener.UserID] = []*EventBusListener{}
|
||||||
}
|
}
|
||||||
bus.TotalRoomListeners[listener.UserID] = append(bus.TotalRoomListeners[listener.UserID], listener)
|
bus.TotalRoomListeners[listener.UserID] = append(bus.TotalRoomListeners[listener.UserID], listener)
|
||||||
log.Printf("Listener added to room: `%s`. %d registered listeners", listener.UserID, len(bus.TotalRoomListeners[listener.UserID]))
|
log.Printf("Listener added to room: `%s`. %d registered listeners", listener.UserID, len(bus.TotalRoomListeners[listener.UserID]))
|
||||||
|
|
|
@ -3,6 +3,7 @@ package models
|
||||||
type EventSourceSyncStatus string
|
type EventSourceSyncStatus string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
EventTypeKeepAlive EventSourceSyncStatus = "keep_alive"
|
||||||
EventTypeSourceSync EventSourceSyncStatus = "source_sync"
|
EventTypeSourceSync EventSourceSyncStatus = "source_sync"
|
||||||
EventTypeSourceComplete EventSourceSyncStatus = "source_complete"
|
EventTypeSourceComplete EventSourceSyncStatus = "source_complete"
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type EventKeepAlive struct {
|
||||||
|
*Event `json:",inline"`
|
||||||
|
Time string `json:"time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEventKeepAlive(userID string) *EventKeepAlive {
|
||||||
|
return &EventKeepAlive{
|
||||||
|
Event: &Event{
|
||||||
|
UserID: userID,
|
||||||
|
EventType: EventTypeKeepAlive,
|
||||||
|
},
|
||||||
|
Time: time.Now().Format("2006-01-02 15:04:05"),
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,7 +23,7 @@ func SSEStream(c *gin.Context) {
|
||||||
log.Printf("could not get client channel from context")
|
log.Printf("could not get client channel from context")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
listener, ok := v.(event_bus.EventBusListener)
|
listener, ok := v.(*event_bus.EventBusListener)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,15 +41,15 @@ func SSEEventBusServerMiddleware(logger *logrus.Entry) gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send new connection to event server
|
// Send new connection to event server
|
||||||
bus.NewListener <- clientListener
|
bus.NewListener <- &clientListener
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
// Send closed connection to event server
|
// Send closed connection to event server
|
||||||
bus.ClosedListener <- clientListener
|
bus.ClosedListener <- &clientListener
|
||||||
}()
|
}()
|
||||||
|
|
||||||
c.Set(pkg.ContextKeyTypeSSEEventBusServer, bus)
|
c.Set(pkg.ContextKeyTypeSSEEventBusServer, &bus)
|
||||||
c.Set(pkg.ContextKeyTypeSSEClientChannel, clientListener)
|
c.Set(pkg.ContextKeyTypeSSEClientChannel, &clientListener)
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,7 @@ export class EventBusService {
|
||||||
this.authService.IsAuthenticatedSubject.subscribe((isAuthenticated) => {
|
this.authService.IsAuthenticatedSubject.subscribe((isAuthenticated) => {
|
||||||
console.log("isAuthenticated changed:", isAuthenticated)
|
console.log("isAuthenticated changed:", isAuthenticated)
|
||||||
if(isAuthenticated){
|
if(isAuthenticated){
|
||||||
|
console.log("Started listening to event bus")
|
||||||
this.eventBusSubscription = this.listenEventBus().subscribe((event: Event | EventSourceSync | EventSourceComplete)=>{
|
this.eventBusSubscription = this.listenEventBus().subscribe((event: Event | EventSourceSync | EventSourceComplete)=>{
|
||||||
console.log("eventbus event:", event)
|
console.log("eventbus event:", event)
|
||||||
//TODO: start toasts.
|
//TODO: start toasts.
|
||||||
|
@ -48,6 +49,7 @@ export class EventBusService {
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
//no longer authenticated, unsubscribe from eventbus and abort/terminate connection
|
//no longer authenticated, unsubscribe from eventbus and abort/terminate connection
|
||||||
|
console.log("Stopped listening to event bus")
|
||||||
this.abortEventBus()
|
this.abortEventBus()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue