ws

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CloseNormalClosure indicates normal closure
	CloseNormalClosure = websocket.CloseNormalClosure

	// CloseInternalServerErr indicates closure because of internal error
	CloseInternalServerErr = websocket.CloseInternalServerErr

	// CloseAbnormalClosure indicates abnormal close
	CloseAbnormalClosure = websocket.CloseAbnormalClosure

	// CloseGoingAway indicates closing because of server shuts down or client disconnects
	CloseGoingAway = websocket.CloseGoingAway
)

Variables

This section is empty.

Functions

func CheckOrigin

func CheckOrigin(origins string) func(r *http.Request) bool

func CloseWithReason

func CloseWithReason(ws *websocket.Conn, code int, reason string)

CloseWithReason closes WebSocket connection with the specified close code and reason

func FetchUID

func FetchUID(r *http.Request) (string, error)

FetchUID safely extracts uid from `X-Request-ID` header or generates a new one

func IsCloseError

func IsCloseError(err error) bool

func WebsocketHandler

func WebsocketHandler(subprotocols []string, headersExtractor HeadersExtractor, config *Config, sessionHandler sessionHandler) http.Handler

WebsocketHandler generate a new http handler for WebSocket connections

Types

type Config

type Config struct {
	ReadBufferSize    int
	WriteBufferSize   int
	MaxMessageSize    int64
	EnableCompression bool
	AllowedOrigins    string
}

Config contains WebSocket connection configuration.

func NewConfig

func NewConfig() Config

NewConfig build a new Config struct

type Connection

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

Connection is a WebSocket implementation of Connection

func NewConnection

func NewConnection(conn *websocket.Conn) *Connection

func (Connection) Close

func (ws Connection) Close(code int, reason string)

Close sends close frame with a given code and a reason

func (Connection) Read

func (ws Connection) Read() ([]byte, error)

func (Connection) Write

func (ws Connection) Write(msg []byte, deadline time.Time) error

Write writes a text message to a WebSocket

func (Connection) WriteBinary

func (ws Connection) WriteBinary(msg []byte, deadline time.Time) error

WriteBinary writes a binary message to a WebSocket

type DefaultHeadersExtractor added in v1.3.0

type DefaultHeadersExtractor struct {
	Headers []string
	Cookies []string
}

func (*DefaultHeadersExtractor) FromRequest added in v1.3.0

func (h *DefaultHeadersExtractor) FromRequest(r *http.Request) map[string]string

type FrameType

type FrameType int
const (
	TextFrame   FrameType = 0
	CloseFrame  FrameType = 1
	BinaryFrame FrameType = 2
)

type HeadersExtractor added in v1.2.3

type HeadersExtractor interface {
	FromRequest(r *http.Request) map[string]string
}

type RequestInfo

type RequestInfo struct {
	UID     string
	URL     string
	Headers *map[string]string
}

func NewRequestInfo

func NewRequestInfo(r *http.Request, extractor HeadersExtractor) (*RequestInfo, error)

type SentFrame

type SentFrame struct {
	FrameType   FrameType
	Payload     []byte
	CloseCode   int
	CloseReason string
}

Jump to

Keyboard shortcuts

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