mirror of
https://github.com/axllent/mailpit.git
synced 2025-01-10 00:43:53 +02:00
eff483c1c4
BREAKING CHANGE: This release includes a major backend storage change (SQLite) that will render any previously-saved messages useless. Please delete old data to free up space. For more information see https://github.com/axllent/mailpit/issues/10
82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
// 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"
|
|
|
|
"github.com/axllent/mailpit/data"
|
|
"github.com/axllent/mailpit/logger"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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)
|
|
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 := data.WebsocketNotification{}
|
|
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 }()
|
|
}
|