Simplified the SSE system!
This commit is contained in:
98
lib/sse.go
98
lib/sse.go
@ -1,14 +1,6 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"atri.dad/lib/pubsub"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
import "sync"
|
||||
|
||||
type SSEServerType struct {
|
||||
clients map[string]map[chan string]bool
|
||||
@ -37,6 +29,8 @@ func (s *SSEServerType) AddClient(channel string, client chan string) {
|
||||
s.clients[channel] = make(map[chan string]bool)
|
||||
}
|
||||
s.clients[channel][client] = true
|
||||
|
||||
LogInfo.Printf("\nClient connected to channel %s\n", channel)
|
||||
}
|
||||
|
||||
func (s *SSEServerType) RemoveClient(channel string, client chan string) {
|
||||
@ -47,6 +41,8 @@ func (s *SSEServerType) RemoveClient(channel string, client chan string) {
|
||||
if len(s.clients[channel]) == 0 {
|
||||
delete(s.clients, channel)
|
||||
}
|
||||
|
||||
LogInfo.Printf("\nClient disconnected from channel %s\n", channel)
|
||||
}
|
||||
|
||||
func (s *SSEServerType) ClientCount(channel string) int {
|
||||
@ -56,87 +52,15 @@ func (s *SSEServerType) ClientCount(channel string) int {
|
||||
return len(s.clients[channel])
|
||||
}
|
||||
|
||||
func SendSSE(ctx context.Context, messageBroker pubsub.PubSub, channel string, message string) error {
|
||||
LogInfo.Printf("Sending SSE message to channel %s", channel)
|
||||
func SendSSE(channel string, message string) error {
|
||||
SSEServer.mu.Lock()
|
||||
defer SSEServer.mu.Unlock()
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errCh <- ctx.Err()
|
||||
default:
|
||||
err := messageBroker.PublishToChannel(channel, message)
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
err := <-errCh
|
||||
if err != nil {
|
||||
LogError.Printf("Error sending SSE message: %v", err)
|
||||
|
||||
return err
|
||||
for client := range SSEServer.clients[channel] {
|
||||
client <- message
|
||||
}
|
||||
|
||||
LogSuccess.Printf("SSE message sent successfully")
|
||||
LogDebug.Printf("\nMessage broadcast on channel %s: %s\n", channel, message)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetSSEHeaders(c echo.Context) {
|
||||
c.Response().Header().Set(echo.HeaderContentType, "text/event-stream")
|
||||
c.Response().Header().Set(echo.HeaderConnection, "keep-alive")
|
||||
c.Response().Header().Set(echo.HeaderCacheControl, "no-cache")
|
||||
c.Response().Header().Set("X-Accel-Buffering", "no")
|
||||
}
|
||||
|
||||
func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client chan string) {
|
||||
if c.Response().Writer == nil {
|
||||
LogError.Printf("Cannot handle incoming messages: ResponseWriter is nil")
|
||||
return
|
||||
}
|
||||
|
||||
var mutex sync.Mutex
|
||||
|
||||
for {
|
||||
// Receive messages using the context of the request, which is cancelled when the client disconnects
|
||||
msg, err := pubsub.ReceiveMessage(c.Request().Context())
|
||||
if err != nil {
|
||||
if err == context.Canceled {
|
||||
// Log when the client disconnects and stop the message forwarding
|
||||
LogInfo.Printf("Client disconnected, stopping message forwarding")
|
||||
return
|
||||
}
|
||||
// Log errors other than client disconnection
|
||||
LogError.Printf("Failed to receive message: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare the data string to be sent as an SSE
|
||||
data := fmt.Sprintf("data: %s\n\n", msg.Payload)
|
||||
|
||||
// Locking before writing to the response writer to avoid concurrent write issues
|
||||
mutex.Lock()
|
||||
if c.Response().Writer != nil {
|
||||
_, err := c.Response().Write([]byte(data))
|
||||
if err != nil {
|
||||
// Log failure to write and unlock before returning
|
||||
LogError.Printf("Failed to write message: %v", err)
|
||||
mutex.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Flush the response if possible
|
||||
flusher, ok := c.Response().Writer.(http.Flusher)
|
||||
if ok {
|
||||
flusher.Flush()
|
||||
} else {
|
||||
LogError.Println("Failed to flush: ResponseWriter does not implement http.Flusher")
|
||||
}
|
||||
} else {
|
||||
LogError.Println("Failed to write: ResponseWriter is nil")
|
||||
}
|
||||
// Ensure the mutex is unlocked after processing each message
|
||||
mutex.Unlock()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user