websocket

package
v0.0.0-...-f0b9b21 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TriedToWriteToAClosedSession = "tried to write to a closed session"
	SessionMessageBufferIsFull   = "session message buffer is full"
)

Variables

View Source
var DefaultConfig = &Config{
	WriteWait:         10 * time.Second,
	PongWait:          45 * time.Second,
	PingPeriod:        10 * time.Second,
	MaxMessageSize:    30 * 1024,
	MessageBufferSize: 64,
}

Functions

func NewHub

func NewHub() *hub

Types

type Config

type Config struct {
	WriteWait         time.Duration
	PongWait          time.Duration
	PingPeriod        time.Duration
	MaxMessageSize    int
	MessageBufferSize int
}

Config configuration struct

type Error

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

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(err error) bool

type Handler

type Handler interface {
	OnError(Session, error)
	OnConnect(Session)
	OnDisconnect(Session)
	OnMessageText(Session, protocol.Payload)
	Close() error
}

Handler is an interface which defines a handler, methods are called upon when an event occurs.

type Hub

type Hub interface {
	BroadcastPayload(protocol.Payload) error
	BroadcastRaw([]byte) error
	Close()
	Registered(session Session) bool
	Register(session Session)
	Unregister(session Session)
}

Hub defines the methods for the hub. The hub is responsible for broadcasting messages amongst registered sessions.

type Session

type Session interface {
	// RemoteAddr is the *http.Request's Real Remote Address (parses X-Forwarded-For or X-Real-IP)
	RemoteAddr() netip.AddrPort
	// Text() returns an io.Writer, writing text messages to the session.
	Text() io.Writer
	Write(messageType int, msg []byte)
	// WriteError writes an error in the custom protocol form. Any error occured during the
	// write is JSON related.
	WriteError(writeError error) (err error)
	WriteJSON(payload any) (err error)
	Close(closeError error)
	Context() context.Context
	Latency() float64
}

type WebSocket

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

WebSocket defines a WebSocket server that's designed with the `anime-pack`'s protocol in mind.

func NewWebSocket

func NewWebSocket(hub Hub) (w *WebSocket)

NewWebSocket returns an initialized WebSocket instance with the default config.

func NewWithConfig

func NewWithConfig(hub Hub, config *Config) (w *WebSocket)

NewWithConfig returns an initialized WebSocket with the specified config.

func (*WebSocket) Close

func (w *WebSocket) Close()

Close will forcefully close out all the other connections.

func (*WebSocket) Hub

func (w *WebSocket) Hub() Hub

Hub returns the WebSocket's hub where it allows the user to be able to broadcast all sorts of messages to connected sessions.

func (*WebSocket) L

func (w *WebSocket) L(ctx context.Context) *zap.Logger

func (*WebSocket) Request

func (w *WebSocket) Request(rw http.ResponseWriter, r *http.Request)

Request provides a `net.http`.Handler to establish a websocket connection

func (*WebSocket) SetupHandler

func (w *WebSocket) SetupHandler(handler Handler)

SetupHandler set's up the provided handler to the websocket in-order to start receiving events.

Jump to

Keyboard shortcuts

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