mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-02-21 13:09:10 -06:00
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
revactx "github.com/cs3org/reva/v2/pkg/ctx"
|
||||
@@ -12,6 +13,12 @@ import (
|
||||
"github.com/owncloud/ocis/v2/services/sse/pkg/config"
|
||||
)
|
||||
|
||||
// ServerSentEvent is the data structure sent by the sse service
|
||||
type ServerSentEvent struct {
|
||||
Type string `json:"type"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
// SSE defines implements the business logic for Service.
|
||||
type SSE struct {
|
||||
c *config.Config
|
||||
@@ -45,20 +52,25 @@ func (s SSE) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// ListenForEvents listens for events
|
||||
func (s SSE) ListenForEvents() error {
|
||||
func (s SSE) ListenForEvents() {
|
||||
for e := range s.evChannel {
|
||||
switch ev := e.Event.(type) {
|
||||
default:
|
||||
s.l.Error().Interface("event", ev).Msg("unhandled event")
|
||||
case events.SendSSE:
|
||||
b, err := json.Marshal(ServerSentEvent{
|
||||
Type: ev.Type,
|
||||
Data: ev.Message,
|
||||
})
|
||||
if err != nil {
|
||||
s.l.Error().Interface("event", ev).Msg("cannot marshal event")
|
||||
continue
|
||||
}
|
||||
s.sse.Publish(ev.UserID, &sse.Event{
|
||||
Event: []byte(ev.Type),
|
||||
Data: ev.Message,
|
||||
Data: b,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandleSSE is the GET handler for events
|
||||
|
||||
Reference in New Issue
Block a user