1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-01-10 00:43:53 +02:00
mailpit/server/websockets/hub.go

87 lines
1.8 KiB
Go
Raw Normal View History

2022-07-29 13:23:08 +02:00
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package websockets
import (
"encoding/json"
2022-10-28 23:52:22 +02:00
"github.com/axllent/mailpit/utils/logger"
2022-07-29 13:23:08 +02:00
)
// Hub maintains the set of active clients and broadcasts messages to the
// clients.
type Hub struct {
// Registered clients.
Clients map[*Client]bool
// Inbound messages from the clients.
Broadcast chan []byte
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
}
// WebsocketNotification struct for responses
type WebsocketNotification struct {
Type string
Data interface{}
}
2022-07-29 13:23:08 +02:00
// NewHub returns a new hub configuration
func NewHub() *Hub {
return &Hub{
Broadcast: make(chan []byte),
register: make(chan *Client),
unregister: make(chan *Client),
Clients: make(map[*Client]bool),
}
}
// Run runs the listener
func (h *Hub) Run() {
for {
select {
case client := <-h.register:
h.Clients[client] = true
case client := <-h.unregister:
if _, ok := h.Clients[client]; ok {
delete(h.Clients, client)
close(client.send)
}
case message := <-h.Broadcast:
// logger.Log().Debugf("[broadcast] %s", message)
2022-07-29 13:23:08 +02:00
for client := range h.Clients {
select {
case client.send <- message:
default:
close(client.send)
delete(h.Clients, client)
}
}
}
}
}
// Broadcast will spawn a broadcast message to all connected clients
func Broadcast(t string, msg interface{}) {
if MessageHub == nil {
return
}
w := WebsocketNotification{}
2022-07-29 13:23:08 +02:00
w.Type = t
w.Data = msg
b, err := json.Marshal(w)
if err != nil {
logger.Log().Errorf("[http] broadcast received invalid data: %s", err)
}
go func() { MessageHub.Broadcast <- b }()
}