hub

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Copyright (c) 2025 Jon Webb. All rights reserved.

Socket hub implementation is inspired by https://github.com/olahol/melody. Copyright (c) 2015 Ola Holmström. All rights reserved. Licensed under the BSD 2-Clause License.

Index

Constants

View Source
const (
	CloseNormalClosure           = 1000
	CloseGoingAway               = 1001
	CloseProtocolError           = 1002
	CloseUnsupportedData         = 1003
	CloseNoStatusReceived        = 1005
	CloseAbnormalClosure         = 1006
	CloseInvalidFramePayloadData = 1007
	ClosePolicyViolation         = 1008
	CloseMessageTooBig           = 1009
	CloseMandatoryExtension      = 1010
	CloseInternalServerErr       = 1011
	CloseServiceRestart          = 1012
	CloseTryAgainLater           = 1013
	CloseTLSHandshake            = 1015
)

Close codes defined in RFC 6455, section 11.7. Duplicate of codes from gorilla/websocket for convenience.

Variables

View Source
var (
	ErrClosed            = errors.New("hub is closed")
	ErrSessionClosed     = errors.New("session is closed")
	ErrWriteClosed       = errors.New("wrote to a closed session")
	ErrMessageBufferFull = errors.New("session message buffer is full")
)

Functions

This section is empty.

Types

type Hub

type Hub struct {
	Upgrader *websocket.Upgrader

	WriteWait          time.Duration
	PongWait           time.Duration
	PingPeriod         time.Duration
	MaxMessageSize     int64
	MessageBufferSize  int
	ConcurrentHandling bool

	HandleMessage           messageHandlerFunc
	HandleBinaryMessage     messageHandlerFunc
	HandleMessageSent       messageHandlerFunc
	HandleBinaryMessageSent messageHandlerFunc
	HandleError             errorHandlerFunc
	HandleClose             closeHandlerFunc
	HandleConnect           sessionHandlerFunc
	HandleDisconnect        sessionHandlerFunc
	HandlePong              sessionHandlerFunc
	// contains filtered or unexported fields
}

func NewHub

func NewHub() *Hub

func (*Hub) Broadcast

func (h *Hub) Broadcast(data []byte) error

func (*Hub) BroadcastBinary

func (h *Hub) BroadcastBinary(data []byte) error

func (*Hub) BroadcastBinaryFilter

func (h *Hub) BroadcastBinaryFilter(data []byte, fn func(*Session) bool) error

func (*Hub) BroadcastBinaryFilterWithDeadline

func (h *Hub) BroadcastBinaryFilterWithDeadline(data []byte, fn func(*Session) bool, deadline time.Duration) error

func (*Hub) BroadcastBinaryMultiple

func (h *Hub) BroadcastBinaryMultiple(data []byte, sessions []*Session) error

func (*Hub) BroadcastBinaryWithDeadline

func (h *Hub) BroadcastBinaryWithDeadline(data []byte, deadline time.Duration) error

func (*Hub) BroadcastFilter

func (h *Hub) BroadcastFilter(data []byte, fn func(*Session) bool) error

func (*Hub) BroadcastFilterWithDeadline

func (h *Hub) BroadcastFilterWithDeadline(data []byte, fn func(*Session) bool, deadline time.Duration) error

func (*Hub) BroadcastMultiple

func (h *Hub) BroadcastMultiple(data []byte, sessions []*Session) error

func (*Hub) BroadcastWithDeadline

func (h *Hub) BroadcastWithDeadline(data []byte, deadline time.Duration) error

func (*Hub) Close

func (h *Hub) Close() error

func (*Hub) CloseWithMessage

func (h *Hub) CloseWithMessage(data []byte) error

func (*Hub) FormatCloseMessage

func (h *Hub) FormatCloseMessage(code int, text string) []byte

func (*Hub) HandleRequest

func (h *Hub) HandleRequest(w http.ResponseWriter, req *http.Request) error

func (*Hub) HandleRequestWithValues

func (h *Hub) HandleRequestWithValues(w http.ResponseWriter, req *http.Request, values map[string]any) error

func (*Hub) IsClosed

func (h *Hub) IsClosed() bool

func (*Hub) Len

func (h *Hub) Len() int

func (*Hub) Sessions

func (h *Hub) Sessions() ([]*Session, error)

type Session

type Session struct {
	Req    *http.Request
	Values map[string]any
	// contains filtered or unexported fields
}

func (*Session) Close

func (s *Session) Close() error

func (*Session) CloseWithMessage

func (s *Session) CloseWithMessage(data []byte) error

func (*Session) Get

func (s *Session) Get(key string) (value any, ok bool)

func (*Session) IsClosed

func (s *Session) IsClosed() bool

func (*Session) Set

func (s *Session) Set(key string, value any)

func (*Session) Unset

func (s *Session) Unset(key string)

func (*Session) WebsocketConn

func (s *Session) WebsocketConn() *websocket.Conn

func (*Session) Write

func (s *Session) Write(data []byte) error

func (*Session) WriteBinary

func (s *Session) WriteBinary(data []byte) error

func (*Session) WriteBinaryWithDeadline

func (s *Session) WriteBinaryWithDeadline(data []byte, deadline time.Duration) error

func (*Session) WriteWithDeadline

func (s *Session) WriteWithDeadline(data []byte, deadline time.Duration) error

Jump to

Keyboard shortcuts

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