websocket
v0.0.0-20240711162211-159a3375b30a
Documentation
pkg.go.dev ↗Constants
MessageText (20 declarations)
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
ErrTooFast (2 declarations)
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
SandboxMessageHandler
func SandboxMessageHandler(ws *T, h Handler, msg *Message)
SandboxSubprotocol runs a MessageHandler to completion
SandboxSubprotocol
func SandboxSubprotocol(f Subprotocol, ws *T)
SandboxSubprotocol runs a subprotocol to completion
Types
AcceptOptionSetter
type AcceptOptionSetter interface {
SetAcceptOption(a *AcceptOptions)
}
WithCompressionMode
func WithCompressionMode(c CompressionMode) AcceptOptionSetter
WithCompressionThreshold
func WithCompressionThreshold(c int) AcceptOptionSetter
WithInsecureSkipVerify
func WithInsecureSkipVerify(b bool) AcceptOptionSetter
WithOriginPatterns
func WithOriginPatterns(s []string) AcceptOptionSetter
AcceptOptions
type AcceptOptions = websocket.AcceptOptions
AcceptOptions = websocket.AcceptOptions
Cache
type Cache struct {
maps.Observable[string, *T]
// contains filtered or unexported fields
}
NewCache
func NewCache(protocol Protocol, keygen func() string, settings ...AcceptOptionSetter) *Cache
Cache.ServeHTTP
func (cache *Cache) ServeHTTP(w http.Writer, r *http.Request)
CompressionMode
type CompressionMode = websocket.CompressionMode
CompressionMode = websocket.CompressionMode
Conn
type Conn = websocket.Conn
Conn = websocket.Conn
Accept
func Accept(w http.Writer, r *http.Request, opt *AcceptOptions) (*Conn, error)
Accept wraps websocket.Accept
Error
type Error interface {
error
StatusCode() StatusCode
}
StatusError
func StatusError(code StatusCode, err string) Error
Fork
type Fork []Pather
Fork is a Pather made of []Pather
NewFork
func NewFork() *Fork
NewFork creates a Fork
Fork.Add
func (f *Fork) Add(p Pather)
Add appends a Pather to this Fork
Fork.Path
func (f *Fork) Path(r Router, h Handler)
Path calls Add with a new Path
Fork.PathFunc
func (f *Fork) PathFunc(r Router, hf func(*T, *Message))
PathFunc calls Path with HandlerFunc(hf)
Fork.ServeWS
func (f *Fork) ServeWS(ws *T, msg *Message)
Framer
type Framer = func(*T, MessageType, io.Reader) error
Framer is a func type for websocket connection handler
MessageFramer
func MessageFramer(s MessageSerializer, handler Handler) Framer
MessageFramer creates a Framer from a MessageHandler
FramerMiddleware
type FramerMiddleware = func(Framer) Framer
Handler
type Handler interface {
ServeWS(*T, *Message)
}
Handler is analogous to http.Handler
Use
func Use(h Handler, m ...Middleware) Handler
Using
func Using(ms []Middleware, h Handler) Handler
HandlerFunc
type HandlerFunc func(*T, *Message)
HandlerFunc is a func type for Handler
HandlerFunc.ServeWS
func (f HandlerFunc) ServeWS(ws *T, msg *Message)
JSON
type JSON = map[string]any
JSON is an alias type for a string-keyed builtin map
Message
type Message struct {
URI string `json:"uri"`
Data JSON `json:"data"`
}
Message is a simple data messaging type for MessageType=MessageText
NewMessage
func NewMessage(uri string, data JSON) *Message
NewMessage creates a structured JSON message with the given type
MessageSerializer
type MessageSerializer interface {
Encode(Message) ([]byte, error)
Decode([]byte) (*Message, error)
}
MessageSerializer marshals and unmarshals Messages
MessageType
type MessageType = websocket.MessageType
MessageType = websocket.MessageType
Middleware
type Middleware = func(next Handler) Handler
Middleware is a consumer type that manipulates Handlers
Observer
type Observer = maps.Observer[string, *T]
ObserverFunc
type ObserverFunc = maps.ObserverFunc[string, *T]
Path
type Path struct {
Router Router
Handler Handler
}
Path is a struct with Handler and Router pointers
NewPath
func NewPath(router Router, handler Handler) Path
NewPath creates a Path
Path.RouteWS
func (p Path) RouteWS(msg *Message) bool
Path.ServeWS
func (p Path) ServeWS(ws *T, msg *Message)
Pather
type Pather interface {
Router
Handler
}
Pather is a Handler and Router
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
MessageProtocol
func MessageProtocol(enc MessageSerializer, handler Handler) Protocol
MessageProtocol returns Protocol from ProtocolFunc and MessageSubprotocol
MessageProtocolName
func MessageProtocolName(subprotoName string, enc MessageSerializer, handler Handler) Protocol
MessageProtocolName returns Protocol from SubprotocolMap and MessageSubprotocol
ProtocolFunc
type ProtocolFunc Subprotocol
ProtocolFunc is a quick-n-dirty Protocol that is only 1 Subprotocol
ProtocolFunc.GetSubprotocol
func (f ProtocolFunc) GetSubprotocol(name string) Subprotocol
GetSubprotocol implements Protocol by returning f when name==""
ProtocolFunc.GetSubprotocols
func (ProtocolFunc) GetSubprotocols() []string
GetSubprotocols implements Protocol
RouteURI
type RouteURI string
RouteURI creates a Router matching Message.URI
RouteURI.RouteWS
func (r RouteURI) RouteWS(msg *Message) bool
Router
type Router interface {
RouteWS(*Message) bool
}
Router is used to route Messages
RouterYes
func RouterYes() Router
RouterYes returns a Router that matches any Message
RouterFunc
type RouterFunc func(*Message) bool
RouterFunc creates a match using a func pointer
RouterFunc.RouteWS
func (f RouterFunc) RouteWS(msg *Message) bool
StatusCode
type StatusCode = websocket.StatusCode
StatusCode = websocket.StatusCode
Subprotocol
type Subprotocol = func(*T) error
Subprotocol is a connection handler
MessageSubprotocol
func MessageSubprotocol(enc MessageSerializer, handler Handler) Subprotocol
MessageSubprotocol returns Subprotocol from NewSubprotocol and MessageFramer
NewSubprotocol
func NewSubprotocol(f Framer) Subprotocol
NewSubprotocol creates a frame-based read-limited Subprotocol
SubprotocolMap
type SubprotocolMap map[string]Subprotocol
SubprotocolMap is builtin map Protocol
SubprotocolMap.GetSubprotocol
func (m SubprotocolMap) GetSubprotocol(name string) Subprotocol
GetSubprotocol implements Protocol
SubprotocolMap.GetSubprotocols
func (m SubprotocolMap) GetSubprotocols() []string
GetSubprotocols implements Protocol
T
type T struct {
// contains filtered or unexported fields
}
T is a Websocket
New
func New(req *http.Request, conn *Conn, id string) *T
New creates a websocket wrapper T
T.Done
func (ws *T) Done() <-chan struct{}
Done returns the done channel
T.ID
func (ws *T) ID() string
ID returns the websocket ID
T.Reader
func (ws *T) Reader() (MessageType, io.Reader, error)
Reader exposes the websocket Reader API and must only be called synchronously
T.Request
func (ws *T) Request() *http.Request
Request returns the original internal request
T.Subprotocol
func (ws *T) Subprotocol() string
Subprotocol returns the name of the negotiated subprotocol
T.Write
func (ws *T) Write(typ MessageType, buf []byte) error
T.WriteBinary
func (ws *T) WriteBinary(buf []byte) error
T.WriteMessage
func (ws *T) WriteMessage(msg *Message) error
T.WriteText
func (ws *T) WriteText(buf []byte) error
T.Writer
func (ws *T) Writer(typ MessageType) (io.WriteCloser, error)
Writer exposes the websocket API and may be called asynchronously