websocket

package
v0.0.0-...-780cc0d Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2022 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContinuationFrame = 0
	TextFrame         = 1
	BinaryFrame       = 2
	CloseFrame        = 8
	PingFrame         = 9
	PongFrame         = 10
	UnknownFrame      = 255
)
View Source
const (
	SupportedProtocolVersion = "13"
	ProtocolVersionHybi13    = 13
	DefaultMaxPayloadBytes   = 32 << 20 // 32MB
)

Variables

View Source
var (
	ErrBadProtocolVersion   = &ProtocolError{"bad protocol version"}
	ErrBadScheme            = &ProtocolError{"bad scheme"}
	ErrBadStatus            = &ProtocolError{"bad status"}
	ErrBadUpgrade           = &ProtocolError{"missing or bad upgrade"}
	ErrBadWebSocketOrigin   = &ProtocolError{"missing or bad WebSocket-Origin"}
	ErrBadWebSocketLocation = &ProtocolError{"missing or bad WebSocket-Location"}
	ErrBadWebSocketProtocol = &ProtocolError{"missing or bad WebSocket-Protocol"}
	ErrBadWebSocketVersion  = &ProtocolError{"missing or bad WebSocket Version"}
	ErrChallengeResponse    = &ProtocolError{"mismatch challenge/response"}
	ErrBadFrame             = &ProtocolError{"bad frame2"}
	ErrBadFrameBoundary     = &ProtocolError{"not on frame2 boundary"}
	ErrNotWebSocket         = &ProtocolError{"not websocket protocol"}
	ErrBadRequestMethod     = &ProtocolError{"bad method"}
	ErrNotSupported         = &ProtocolError{"not supported"}
	ErrFrameTooLarge        = &ProtocolError{"websocket: frame2 payload size exceeds limiter"}

	ErrBadMaskingKey         = &ProtocolError{"bad masking key"}
	ErrBadPongMessage        = &ProtocolError{"bad pong message"}
	ErrBadClosingStatus      = &ProtocolError{"bad closing status"}
	ErrUnsupportedExtensions = &ProtocolError{"unsupported extensions"}
	ErrNotImplemented        = &ProtocolError{"not implemented"}
	ErrSetDeadline           = &ProtocolError{"websocket: cannot set deadline: not using a net.Conn"}
)
View Source
var JSON = Codec{jsonMarshal, jsonUnmarshal}
View Source
var Message = Codec{marshal, unmarshal}

Functions

func CheckOrigin

func CheckOrigin(config *ConnConfig, req *http.Request) (err error)

func DefaultEncodeResponse

func DefaultEncodeResponse(ctx context.Context, writer *Conn, response any) error

func DefaultErrorEncoder

func DefaultErrorEncoder(ctx context.Context, err error, writer *Conn)

func NewResponse

func NewResponse(success bool, code int, message string, data any) error

NewResponse 实例化

func Origin

func Origin(config *ConnConfig, req *http.Request) (*url.URL, error)

Types

type Addr

type Addr struct {
	*url.URL
}

func (*Addr) Network

func (addr *Addr) Network() string

type Codec

type Codec struct {
	Marshal   func(v any) (data []byte, payloadType byte, err error)
	Unmarshal func(data []byte, payloadType byte, v any) (err error)
}

func (Codec) Receive

func (c Codec) Receive(ws *Conn, v any) (err error)

func (Codec) Send

func (c Codec) Send(ws *Conn, v any) (err error)

type Conn

type Conn struct {
	PayloadType byte

	MaxPayloadBytes int
	// contains filtered or unexported fields
}

func NewConn

func NewConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Request, config *ConnConfig, handshake func(*ConnConfig, *http.Request) error) (conn *Conn, err error)

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) Config

func (c *Conn) Config() *ConnConfig

func (*Conn) IsClientConn

func (c *Conn) IsClientConn() bool

func (*Conn) IsServerConn

func (c *Conn) IsServerConn() bool

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

func (*Conn) Read

func (c *Conn) Read(msg []byte) (n int, err error)

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

func (*Conn) Request

func (c *Conn) Request() *http.Request

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

func (*Conn) Write

func (c *Conn) Write(msg []byte) (n int, err error)

type ConnConfig

type ConnConfig struct {
	Location  *url.URL    // websocket服务端地址
	Origin    *url.URL    // websocket客户端地址
	Protocol  []string    // 协议
	Version   int         // 协议版本
	TlsConfig *tls.Config // wss的配置
	Header    http.Header // Additional header fields to be sent in WebSocket opening handshake.
	Dialer    *net.Dialer // Dialer used when opening websocket connections.
	// contains filtered or unexported fields
}

type DecodeRequestFunc

type DecodeRequestFunc func(ctx context.Context, r *Conn) (req any, err error)

type EncodeResponseFunc

type EncodeResponseFunc func(ctx context.Context, w *Conn, res any) error

type ErrorEncoder

type ErrorEncoder func(ctx context.Context, err error, w *Conn)

type Handler

type Handler struct {
	DecodeRequest  DecodeRequestFunc
	EncodeResponse EncodeResponseFunc
	ErrorEncoder   ErrorEncoder
	Endpoint       endpoint.Endpoint
}

func NewHandler

func NewHandler(decode DecodeRequestFunc, endpoint endpoint.Endpoint) *Handler

NewHandler 实例化处理

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ProtocolError

type ProtocolError struct {
	ErrorString string
}

func (*ProtocolError) Error

func (err *ProtocolError) Error() string

type Response

type Response struct {
	Success bool   `json:"success"`
	Code    int    `json:"code"`
	Message string `json:"message,omitempty"`
	Data    any    `json:"datas,omitempty"`
}

func (Response) Error

func (s Response) Error() string

type Server

type Server struct {
	Config  ServerConfig
	Handler http.Handler
	Limiter *limiter.Limiter
	Nacos   *nacos.Nacos
}

func NewServer

func NewServer(c ServerConfig, h http.Handler, l *limiter.Limiter, n *nacos.Nacos) *Server

NewServer websocket的服务端

func (*Server) Run

func (s *Server) Run() error

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ServerConfig

type ServerConfig struct {
	Name         string `mapstructure:"name"`
	Host         string `mapstructure:"host"`
	Port         string `mapstructure:"port"`
	ReadTimeout  int64  `mapstructure:"readTimeout"`
	WriteTimeout int64  `mapstructure:"writeTimeout"`
	Metadata     map[string]string
}

Jump to

Keyboard shortcuts

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