Documentation ¶
Index ¶
- type Client
- type Herald
- func (h *Herald) AddClient(w http.ResponseWriter, r *http.Request, data interface{}) (*Client, error)
- func (h *Herald) Clients() []*Client
- func (h *Herald) Close()
- func (h *Herald) Send(message *Message, clients []*Client)
- func (h *Herald) SetCheckOrigin(fn func(*http.Request) bool)
- func (h *Herald) Start()
- type Message
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { Data interface{} // contains filtered or unexported fields }
Client maintains information about an active client.
type Herald ¶
type Herald struct { // MessageHandler processes messages as they are coming in. If nil, // messages will simply be re-broadcast to all clients. MessageHandler func(message *Message, client *Client) // ClientAddedHandler processes new clients after they connect. This field // is optional. ClientAddedHandler func(client *Client) // ClientRemovedHandler processes clients after they disconnect. This field // is optional. ClientRemovedHandler func(client *Client) // contains filtered or unexported fields }
Herald maintains a set of WebSocket connections and facilitates the exchange of messages between them.
func New ¶
func New() *Herald
New creates and begins initializing a new Herald instance. The Herald is not started until the Start() method is invoked.
func (*Herald) AddClient ¶
func (h *Herald) AddClient(w http.ResponseWriter, r *http.Request, data interface{}) (*Client, error)
AddClient adds a new WebSocket client and begins exchanging messages.
func (*Herald) Close ¶
func (h *Herald) Close()
Close disconnects all clients and stops exchanging messages.
func (*Herald) Send ¶
Send sends the specified message to the client specified in the message or all clients if nil. The send operation takes place in a separate goroutine to enable the call to be made from handlers without triggering a deadlock.
func (*Herald) SetCheckOrigin ¶
SetCheckOrigin provides a function that will be invoked for every new connection. If the function returns true, it will be allowed.
type Message ¶
type Message struct { Type string `json:"type"` Data json.RawMessage `json:"data"` }
Message stores information for broadcasting to other clients. The Client field is a pointer to either the client who sent the message or the one that should receive it.
func NewMessage ¶
NewMessage creates a new Message instance of the specified type with the specified data.