websockethub

package
v0.0.0-...-15a9106 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2023 License: Apache-2.0, BSD-2-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWebsocketServerUnavailable = errors.New("websocket server unavailable")
	ErrClientDisconnected         = errors.New("client was disconnected")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	*logger.WrappedLogger

	// a channel which is closed when the websocket client is disconnected.
	ExitSignal chan struct{}

	// channel of inbound messages.
	// this will be created by the user if receiving messages is needed.
	ReceiveChan chan *WebsocketMsg

	// FilterCallback is used to filter messages to clients on BroadcastMsg
	FilterCallback func(c *Client, data interface{}) bool
	// contains filtered or unexported fields
}

Client is a middleman between the node and the websocket connection.

func NewClient

func NewClient(hub *Hub, conn *websocket.Conn, onConnect func(client *Client), onDisconnect func(client *Client)) *Client

func (*Client) Context

func (c *Client) Context() context.Context

Context returns the client context which is canceled when the ping times out and the client should be dropped.

func (*Client) ID

func (c *Client) ID() ClientID

ID returns the id of the client.

func (*Client) Send

func (c *Client) Send(ctx context.Context, msg interface{}, dontDrop ...bool) error

Send sends a message to the client.

type ClientConnectionEvent

type ClientConnectionEvent struct {
	ID ClientID
}

type ClientID

type ClientID uint32

ClientID is the ID of a client.

type Events

type Events struct {
	// A ClientConnected event is triggered, when a new client has connected to the websocket hub.
	ClientConnected *event.Event1[*ClientConnectionEvent]
	// A ClientDisconnected event is triggered, when a client has disconnected from the websocket hub.
	ClientDisconnected *event.Event1[*ClientConnectionEvent]
}

Events contains all the events that are triggered by the websocket hub.

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub maintains the set of active clients and broadcasts messages to the clients.

func NewHub

func NewHub(logger *logger.Logger, acceptOptions *websocket.AcceptOptions, broadcastQueueSize int, clientSendChannelSize int, clientReadLimit int64) *Hub

func (*Hub) BroadcastMsg

func (h *Hub) BroadcastMsg(ctx context.Context, data interface{}, dontDrop ...bool) error

BroadcastMsg sends a message to all clients.

func (*Hub) Clients

func (h *Hub) Clients() int

Returns the number of websocket clients.

func (*Hub) Events

func (h *Hub) Events() *Events

Events returns all the events that are triggered by the websocket hub.

func (*Hub) Register

func (h *Hub) Register(client *Client) error

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run starts the hub.

func (*Hub) ServeWebsocket

func (h *Hub) ServeWebsocket(
	w http.ResponseWriter,
	r *http.Request,
	onCreate func(client *Client),
	onConnect func(client *Client),
	onDisconnect func(client *Client)) error

ServeWebsocket handles websocket requests from the peer. onCreate gets called when the client is created. onConnect gets called when the client was registered.

func (*Hub) Stopped

func (h *Hub) Stopped() bool

func (*Hub) Unregister

func (h *Hub) Unregister(client *Client) error

type WebsocketMsg

type WebsocketMsg struct {
	// MsgType is the type of the message based on RFC 6455.
	MsgType websocket.MessageType
	// Data is the received data of the message.
	Data []byte
}

WebsocketMsg is a message received via websocket.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL