websocket

package
v0.0.0-...-159a337 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MessageText = websocket.MessageText
	MessageText = websocket.MessageText
	// MessageBinary = websocket.MessageBinary
	MessageBinary = websocket.MessageBinary

	// CompressionNoContextTakeover = websocket.CompressionNoContextTakeover
	CompressionNoContextTakeover = websocket.CompressionNoContextTakeover
	// CompressionContextTakeover = websocket.CompressionContextTakeover
	CompressionContextTakeover = websocket.CompressionContextTakeover
	// CompressionDisabled = websocket.CompressionDisabled
	CompressionDisabled = websocket.CompressionDisabled

	// StatusNormalClosure StatusCode = 1000
	StatusNormalClosure = websocket.StatusNormalClosure
	// StatusGoingAway StatusCode = 1001
	StatusGoingAway = websocket.StatusGoingAway
	// StatusProtocolError StatusCode = 1002
	StatusProtocolError = websocket.StatusProtocolError
	// StatusUnsupportedData StatusCode = 1003
	StatusUnsupportedData = websocket.StatusUnsupportedData
	// StatusNoStatusRcvd StatusCode = 1005
	StatusNoStatusRcvd = websocket.StatusNoStatusRcvd
	// StatusAbnormalClosure StatusCode = 1006
	StatusAbnormalClosure = websocket.StatusAbnormalClosure
	// StatusInvalidFramePayloadData StatusCode = 1007
	StatusInvalidFramePayloadData = websocket.StatusInvalidFramePayloadData
	// StatusPolicyViolation StatusCode = 1008
	StatusPolicyViolation = websocket.StatusPolicyViolation
	// StatusMessageTooBig StatusCode = 1009
	StatusMessageTooBig = websocket.StatusMessageTooBig
	// StatusMandatoryExtension StatusCode = 1010
	StatusMandatoryExtension = websocket.StatusMandatoryExtension
	// StatusInternalError StatusCode = 1011
	StatusInternalError = websocket.StatusInternalError
	// StatusServiceRestart StatusCode = 1012
	StatusServiceRestart = websocket.StatusServiceRestart
	// StatusTryAgainLater StatusCode = 1013
	StatusTryAgainLater = websocket.StatusTryAgainLater
	// StatusBadGateway StatusCode = 1014
	StatusBadGateway = websocket.StatusBadGateway
	// StatusTLSHandshake StatusCode = 1015
	StatusTLSHandshake = websocket.StatusTLSHandshake
)

Variables

View Source
var (
	// ErrTooFast indicates the connection is sending too fast
	ErrTooFast = StatusError(StatusPolicyViolation, "websocket: too fast")

	// ErrDataType indicates the connection sent badly typed data
	ErrDataType = StatusError(StatusInvalidFramePayloadData, "websocket: data type")
)

Functions

func SandboxMessageHandler

func SandboxMessageHandler(ws *T, h Handler, msg *Message)

SandboxSubprotocol runs a MessageHandler to completion

func SandboxSubprotocol

func SandboxSubprotocol(f Subprotocol, ws *T)

SandboxSubprotocol runs a subprotocol to completion

Types

type AcceptOptionSetter

type AcceptOptionSetter interface {
	SetAcceptOption(a *AcceptOptions)
}

func WithCompressionMode

func WithCompressionMode(c CompressionMode) AcceptOptionSetter

func WithCompressionThreshold

func WithCompressionThreshold(c int) AcceptOptionSetter

func WithInsecureSkipVerify

func WithInsecureSkipVerify(b bool) AcceptOptionSetter

func WithOriginPatterns

func WithOriginPatterns(s []string) AcceptOptionSetter

type AcceptOptions

type AcceptOptions = websocket.AcceptOptions

AcceptOptions = websocket.AcceptOptions

type Cache

type Cache struct {
	maps.Observable[string, *T]
	// contains filtered or unexported fields
}

func NewCache

func NewCache(protocol Protocol, keygen func() string, settings ...AcceptOptionSetter) *Cache

func (*Cache) ServeHTTP

func (cache *Cache) ServeHTTP(w http.Writer, r *http.Request)

type CompressionMode

type CompressionMode = websocket.CompressionMode

CompressionMode = websocket.CompressionMode

type Conn

type Conn = websocket.Conn

Conn = websocket.Conn

func Accept

func Accept(w http.Writer, r *http.Request, opt *AcceptOptions) (*Conn, error)

Accept wraps websocket.Accept

type Error

type Error interface {
	error
	StatusCode() StatusCode
}

func StatusError

func StatusError(code StatusCode, err string) Error

type Fork

type Fork []Pather

Fork is a Pather made of []Pather

func NewFork

func NewFork() *Fork

NewFork creates a Fork

func (*Fork) Add

func (f *Fork) Add(p Pather)

Add appends a Pather to this Fork

func (*Fork) Path

func (f *Fork) Path(r Router, h Handler)

Path calls Add with a new Path

func (*Fork) PathFunc

func (f *Fork) PathFunc(r Router, hf func(*T, *Message))

PathFunc calls Path with HandlerFunc(hf)

func (*Fork) ServeWS

func (f *Fork) ServeWS(ws *T, msg *Message)

type Framer

type Framer = func(*T, MessageType, io.Reader) error

Framer is a func type for websocket connection handler

func MessageFramer

func MessageFramer(s MessageSerializer, handler Handler) Framer

MessageFramer creates a Framer from a MessageHandler

type FramerMiddleware

type FramerMiddleware = func(Framer) Framer

type Handler

type Handler interface {
	ServeWS(*T, *Message)
}

Handler is analogous to http.Handler

func Use

func Use(h Handler, m ...Middleware) Handler

func Using

func Using(ms []Middleware, h Handler) Handler

type HandlerFunc

type HandlerFunc func(*T, *Message)

HandlerFunc is a func type for Handler

func (HandlerFunc) ServeWS

func (f HandlerFunc) ServeWS(ws *T, msg *Message)

type JSON

type JSON = map[string]any

JSON is an alias type for a string-keyed builtin map

type Message

type Message struct {
	URI  string `json:"uri"`
	Data JSON   `json:"data"`
}

Message is a simple data messaging type for MessageType=MessageText

func NewMessage

func NewMessage(uri string, data JSON) *Message

NewMessage creates a structured JSON message with the given type

type MessageSerializer

type MessageSerializer interface {
	Encode(Message) ([]byte, error)
	Decode([]byte) (*Message, error)
}

MessageSerializer marshals and unmarshals Messages

type MessageType

type MessageType = websocket.MessageType

MessageType = websocket.MessageType

type Middleware

type Middleware = func(next Handler) Handler

Middleware is a consumer type that manipulates Handlers

type Observer

type Observer = maps.Observer[string, *T]

type ObserverFunc

type ObserverFunc = maps.ObserverFunc[string, *T]

type Path

type Path struct {
	Router  Router
	Handler Handler
}

Path is a struct with Handler and Router pointers

func NewPath

func NewPath(router Router, handler Handler) Path

NewPath creates a Path

func (Path) RouteWS

func (p Path) RouteWS(msg *Message) bool

func (Path) ServeWS

func (p Path) ServeWS(ws *T, msg *Message)

type Pather

type Pather interface {
	Router
	Handler
}

Pather is a Handler and Router

type Protocol

type Protocol interface {
	// GetSubprotocols returns the list of supported Subprotocol names
	GetSubprotocols() []string

	// GetSubprotocol returns the Subprotocol
	GetSubprotocol(string) Subprotocol
}

Protocol is an interface for acquiring Subprotocol

func MessageProtocol

func MessageProtocol(enc MessageSerializer, handler Handler) Protocol

MessageProtocol returns Protocol from ProtocolFunc and MessageSubprotocol

func MessageProtocolName

func MessageProtocolName(subprotoName string, enc MessageSerializer, handler Handler) Protocol

MessageProtocolName returns Protocol from SubprotocolMap and MessageSubprotocol

type ProtocolFunc

type ProtocolFunc Subprotocol

ProtocolFunc is a quick-n-dirty Protocol that is only 1 Subprotocol

func (ProtocolFunc) GetSubprotocol

func (f ProtocolFunc) GetSubprotocol(name string) Subprotocol

GetSubprotocol implements Protocol by returning f when name==""

func (ProtocolFunc) GetSubprotocols

func (ProtocolFunc) GetSubprotocols() []string

GetSubprotocols implements Protocol

type RouteURI

type RouteURI string

RouteURI creates a Router matching Message.URI

func (RouteURI) RouteWS

func (r RouteURI) RouteWS(msg *Message) bool

type Router

type Router interface {
	RouteWS(*Message) bool
}

Router is used to route Messages

func RouterYes

func RouterYes() Router

RouterYes returns a Router that matches any Message

type RouterFunc

type RouterFunc func(*Message) bool

RouterFunc creates a match using a func pointer

func (RouterFunc) RouteWS

func (f RouterFunc) RouteWS(msg *Message) bool

type StatusCode

type StatusCode = websocket.StatusCode

StatusCode = websocket.StatusCode

type Subprotocol

type Subprotocol = func(*T) error

Subprotocol is a connection handler

func MessageSubprotocol

func MessageSubprotocol(enc MessageSerializer, handler Handler) Subprotocol

MessageSubprotocol returns Subprotocol from NewSubprotocol and MessageFramer

func NewSubprotocol

func NewSubprotocol(f Framer) Subprotocol

NewSubprotocol creates a frame-based read-limited Subprotocol

type SubprotocolMap

type SubprotocolMap map[string]Subprotocol

SubprotocolMap is builtin map Protocol

func (SubprotocolMap) GetSubprotocol

func (m SubprotocolMap) GetSubprotocol(name string) Subprotocol

GetSubprotocol implements Protocol

func (SubprotocolMap) GetSubprotocols

func (m SubprotocolMap) GetSubprotocols() []string

GetSubprotocols implements Protocol

type T

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

T is a Websocket

func New

func New(req *http.Request, conn *Conn, id string) *T

New creates a websocket wrapper T

func (*T) Done

func (ws *T) Done() <-chan struct{}

Done returns the done channel

func (*T) ID

func (ws *T) ID() string

ID returns the websocket ID

func (*T) Reader

func (ws *T) Reader() (MessageType, io.Reader, error)

Reader exposes the websocket Reader API and must only be called synchronously

func (*T) Request

func (ws *T) Request() *http.Request

Request returns the original internal request

func (*T) Subprotocol

func (ws *T) Subprotocol() string

Subprotocol returns the name of the negotiated subprotocol

func (*T) Write

func (ws *T) Write(typ MessageType, buf []byte) error

func (*T) WriteBinary

func (ws *T) WriteBinary(buf []byte) error

func (*T) WriteMessage

func (ws *T) WriteMessage(msg *Message) error

func (*T) WriteText

func (ws *T) WriteText(buf []byte) error

func (*T) Writer

func (ws *T) Writer(typ MessageType) (io.WriteCloser, error)

Writer exposes the websocket API and may be called asynchronously

Directories

Path Synopsis
framer
message_serializer

Jump to

Keyboard shortcuts

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