websocket

package
v1.5.8 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 24 Imported by: 47

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUpgradeTokenNotFound .
	ErrUpgradeTokenNotFound = errors.New("websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header")

	// ErrUpgradeMethodIsGet .
	ErrUpgradeMethodIsGet = errors.New("websocket: the client is not using the websocket protocol: request method is not GET")

	// ErrUpgradeInvalidWebsocketVersion .
	ErrUpgradeInvalidWebsocketVersion = errors.New("websocket: unsupported version: 13 not found in 'Sec-Websocket-Version' header")

	// ErrUpgradeUnsupportedExtensions .
	ErrUpgradeUnsupportedExtensions = errors.New("websocket: application specific 'Sec-WebSocket-Extensions' headers are unsupported")

	// ErrUpgradeOriginNotAllowed .
	ErrUpgradeOriginNotAllowed = errors.New("websocket: request origin not allowed by Upgrader.CheckOrigin")

	// ErrUpgradeMissingWebsocketKey .
	ErrUpgradeMissingWebsocketKey = errors.New("websocket: not a websocket handshake: 'Sec-WebSocket-Key' header is missing or blank")

	// ErrUpgradeNotHijacker .
	ErrUpgradeNotHijacker = errors.New("websocket: response does not implement http.Hijacker")

	// ErrInvalidControlFrame .
	ErrInvalidControlFrame = errors.New("websocket: invalid control frame")

	// ErrInvalidWriteCalling .
	ErrInvalidWriteCalling = errors.New("websocket: invalid write calling, should call WriteMessage instead")

	// ErrReserveBitSet .
	ErrReserveBitSet = errors.New("websocket: reserved bit set it frame")

	// ErrReservedMessageType .
	ErrReservedMessageType = errors.New("websocket: reserved message type received")

	// ErrControlMessageFragmented .
	ErrControlMessageFragmented = errors.New("websocket: control messages must not be fragmented")

	// ErrControlMessageTooBig .
	ErrControlMessageTooBig = errors.New("websocket: control frame length > 125")

	// ErrFragmentsShouldNotHaveBinaryOrTextMessage .
	ErrFragmentsShouldNotHaveBinaryOrTextMessage = errors.New("websocket: fragments should not have message type of text or binary")

	// ErrInvalidCloseCode .
	ErrInvalidCloseCode = errors.New("websocket: invalid close code")

	// ErrBadHandshake .
	ErrBadHandshake = errors.New("websocket: bad handshake")

	// ErrInvalidCompression .
	ErrInvalidCompression = errors.New("websocket: invalid compression negotiation")

	// ErrInvalidUtf8 .
	ErrInvalidUtf8 = errors.New("websocket: invalid UTF-8 bytes")

	// ErrInvalidFragmentMessage .
	ErrInvalidFragmentMessage = errors.New("invalid fragment message")

	// ErrMalformedURL .
	ErrMalformedURL = errors.New("websocket: malformed ws or wss URL")

	// ErrMessageTooLarge.
	ErrMessageTooLarge = errors.New("message exceeds the configured limit")

	// ErrMessageSendQuqueIsFull .
	ErrMessageSendQuqueIsFull = errors.New("message send queue is full")
)
View Source
var (
	// DefaultBlockingReadBufferSize .
	DefaultBlockingReadBufferSize = 1024 * 4

	// DefaultBlockingModAsyncWrite .
	DefaultBlockingModAsyncWrite = true

	// DefaultBlockingModHandleRead .
	DefaultBlockingModHandleRead = true

	// DefaultBlockingModTransferConnToPoller .
	DefaultBlockingModTransferConnToPoller = false

	// DefaultBlockingModSendQueueInitSize .
	DefaultBlockingModSendQueueInitSize = 4

	// DefaultBlockingModSendQueueMaxSize .
	DefaultBlockingModSendQueueMaxSize uint16 = 0

	DefaultBlockingModAsyncCloseDelay = time.Second / 10

	// DefaultEngine will be set to a Upgrader.Engine to handle details such as buffers.
	DefaultEngine = nbhttp.NewEngine(nbhttp.Config{
		ReleaseWebsocketPayload: true,
	})
)

Functions

func CloseCode added in v1.3.17

func CloseCode(err error) int

CloseCode .

func CloseReason added in v1.3.17

func CloseReason(err error) string

CloseReason .

Types

type CloseError added in v1.3.17

type CloseError struct {
	Code   int
	Reason string
}

CloseError .

func (CloseError) Error added in v1.3.17

func (ce CloseError) Error() string

Error .

type Conn

type Conn struct {
	net.Conn

	Engine  *nbhttp.Engine
	Execute func(f func()) bool
	// contains filtered or unexported fields
}

Conn .

func NewClientConn added in v1.4.0

func NewClientConn(opt *Options, c net.Conn, subprotocol string, remoteCompressionEnabled bool, asyncWrite bool) *Conn

func NewServerConn added in v1.4.0

func NewServerConn(u *Upgrader, c net.Conn, subprotocol string, remoteCompressionEnabled bool, asyncWrite bool) *Conn

func (*Conn) Close added in v1.3.16

func (c *Conn) Close() error

Close .

func (*Conn) CloseAndClean added in v1.3.16

func (c *Conn) CloseAndClean(err error)

CloseAndClean .

func (*Conn) CloseWithError added in v1.3.17

func (c *Conn) CloseWithError(err error)

CloseWithError .

func (*Conn) CompressionEnabled added in v1.3.16

func (c *Conn) CompressionEnabled() bool

CompressionEnabled .

func (*Conn) EnableCompression added in v1.3.16

func (c *Conn) EnableCompression(enable bool)

EnableCompression .

func (*Conn) EnableWriteCompression added in v1.1.2

func (c *Conn) EnableWriteCompression(enable bool)

EnableWriteCompression .

func (*Conn) HandleRead added in v1.3.19

func (c *Conn) HandleRead(bufSize int)

HandleRead .

func (*Conn) IsAsyncWrite added in v1.3.17

func (c *Conn) IsAsyncWrite() bool

IsAsyncWrite .

func (*Conn) IsBlockingMod added in v1.3.17

func (c *Conn) IsBlockingMod() bool

IsBlockingMod .

func (*Conn) IsClient added in v1.3.17

func (c *Conn) IsClient() bool

IsClient .

func (*Conn) OnClose

func (c *Conn) OnClose(h func(*Conn, error))

func (*Conn) OnDataFrame added in v1.1.2

func (c *Conn) OnDataFrame(h func(*Conn, MessageType, bool, []byte))

OnDataFrame .

func (*Conn) OnMessage

func (c *Conn) OnMessage(h func(*Conn, MessageType, []byte))

OnMessage .

func (*Conn) Parse added in v1.5.4

func (c *Conn) Parse(data []byte) error

Read .

func (*Conn) Session added in v1.1.3

func (c *Conn) Session() interface{}

Session returns user session.

func (*Conn) SessionWithContext added in v1.3.19

func (c *Conn) SessionWithContext(ctx context.Context) interface{}

SessionWithContext returns user session, returns as soon as the session has been seted or waits until the context is done.

func (*Conn) SessionWithLock added in v1.3.19

func (c *Conn) SessionWithLock() interface{}

SessionWithLock returns user session with lock, returns as soon as the session has been seted.

func (*Conn) SetClient added in v1.3.17

func (c *Conn) SetClient(isClient bool)

SetClient .

func (*Conn) SetCloseError added in v1.3.17

func (c *Conn) SetCloseError(err error)

SetCloseError .

func (*Conn) SetSession added in v1.1.3

func (c *Conn) SetSession(session interface{})

SetSession sets user session.

func (*Conn) Subprotocol added in v1.2.21

func (c *Conn) Subprotocol() string

Subprotocol returns the negotiated websocket subprotocol.

func (*Conn) UnderlayerConn added in v1.4.0

func (c *Conn) UnderlayerConn() net.Conn

func (*Conn) Write

func (c *Conn) Write(data []byte) (int, error)

Write overwrites nbio.Conn.Write.

func (*Conn) WriteClose added in v1.3.17

func (c *Conn) WriteClose(code int, reason string) error

WriteClose .

func (*Conn) WriteFrame added in v1.1.2

func (c *Conn) WriteFrame(messageType MessageType, sendOpcode, fin bool, data []byte) error

WriteFrame .

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(messageType MessageType, data []byte) error

WriteMessage .

type Dialer added in v1.2.4

type Dialer struct {
	Engine *nbhttp.Engine

	Options  *Options
	Upgrader *Upgrader

	Jar http.CookieJar

	DialTimeout time.Duration

	TLSClientConfig *tls.Config

	Proxy func(*http.Request) (*url.URL, error)

	CheckRedirect func(req *http.Request, via []*http.Request) error

	Subprotocols []string

	EnableCompression bool

	Cancel context.CancelFunc
}

Dialer .

func (*Dialer) Dial added in v1.2.4

func (d *Dialer) Dial(urlStr string, requestHeader http.Header, v ...interface{}) (*Conn, *http.Response, error)

Dial .

func (*Dialer) DialContext added in v1.2.4

func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header, v ...interface{}) (*Conn, *http.Response, error)

DialContext .

type MessageType added in v1.1.0

type MessageType int8

MessageType .

const (
	// FragmentMessage .
	FragmentMessage MessageType = 0 // Must be preceded by Text or Binary message
	// TextMessage .
	TextMessage MessageType = 1
	// BinaryMessage .
	BinaryMessage MessageType = 2
	// CloseMessage .
	CloseMessage MessageType = 8
	// PingMessage .
	PingMessage MessageType = 9
	// PongMessage .
	PongMessage MessageType = 10
)

The message types are defined in RFC 6455, section 11.8.t .

type Options added in v1.4.0

type Options = Upgrader

func NewOptions added in v1.4.0

func NewOptions() *Options

type Upgrader

type Upgrader struct {

	// Engine .
	Engine *nbhttp.Engine

	// Subprotocols .
	Subprotocols []string

	// CheckOrigin .
	CheckOrigin func(r *http.Request) bool

	// HandshakeTimeout represents the timeout duration during websocket handshake.
	HandshakeTimeout time.Duration

	// BlockingModReadBufferSize represents the read buffer size of a Conn if it's in blocking mod.
	BlockingModReadBufferSize int

	// BlockingModAsyncWrite represents whether use a goroutine to handle writing:
	// true: use dynamic goroutine to handle writing.
	// false: write buffer to the conn directely.
	BlockingModAsyncWrite bool

	// BlockingModHandleRead represents whether start a goroutine to handle reading automatically during `Upgrade“:
	// true: use dynamic goroutine to handle writing.
	// false: write buffer to the conn directely.
	//
	//
	// Notice:
	// If we start a goroutine to handle read during `Upgrade`, we may receive a new websocket message
	// before we have left the http.Handler for the `Websocket Handshake`.
	// Then if we have the logic of `websocket.Conn.SetSession` in the http.Handler, it's possible that when we receive
	// and are handling a websocket message and call `websocket.Conn.Session()`, we get nil.
	//
	// To fix this nil session problem, can use `websocket.Conn.SessionWithLock()`.
	//
	// For other concurrent problems(including the nil session problem), we can:
	// 1st: set this `BlockingModHandleRead = false`
	// 2nd: `go wsConn.HandleRead(YourBufSize)` after `Upgrade` and finished initialization.
	// Then the websocket message wouldn't come before the http.Handler for `Websocket Handshake` has done.
	BlockingModHandleRead bool

	// BlockingModTrasferConnToPoller represents whether try to transfer a blocking connection to nonblocking and add to `Engine“.
	// true: try to transfer.
	// false: don't try to transfer.
	//
	// Notice:
	// Only `net.TCPConn` and `llib's blocking tls.Conn` can be transferred to nonblocking.
	BlockingModTrasferConnToPoller bool

	// BlockingModSendQueueInitSize represents the init size of a Conn's send queue,
	// only takes effect when `BlockingModAsyncWrite` is true.
	BlockingModSendQueueInitSize int

	// BlockingModSendQueueInitSize represents the max size of a Conn's send queue,
	// only takes effect when `BlockingModAsyncWrite` is true.
	BlockingModSendQueueMaxSize uint16
	// contains filtered or unexported fields
}

Upgrader .

func NewUpgrader

func NewUpgrader() *Upgrader

NewUpgrader .

func (*Upgrader) EnableCompression added in v1.1.2

func (u *Upgrader) EnableCompression(enable bool)

EnableCompression .

func (*Upgrader) OnClose added in v1.1.22

func (u *Upgrader) OnClose(h func(*Conn, error))

OnClose .

func (*Upgrader) OnDataFrame added in v1.1.22

func (u *Upgrader) OnDataFrame(h func(*Conn, MessageType, bool, []byte))

OnDataFrame .

func (*Upgrader) OnMessage added in v1.1.22

func (u *Upgrader) OnMessage(h func(*Conn, MessageType, []byte))

OnMessage .

func (*Upgrader) OnOpen added in v1.1.22

func (u *Upgrader) OnOpen(h func(*Conn))

OnOpen .

func (*Upgrader) SetCloseHandler added in v1.1.22

func (u *Upgrader) SetCloseHandler(h func(*Conn, int, string))

SetCloseHandler .

func (*Upgrader) SetCompressionLevel added in v1.1.22

func (u *Upgrader) SetCompressionLevel(level int) error

SetCompressionLevel .

func (*Upgrader) SetPingHandler added in v1.1.22

func (u *Upgrader) SetPingHandler(h func(*Conn, string))

SetPingHandler .

func (*Upgrader) SetPongHandler added in v1.1.22

func (u *Upgrader) SetPongHandler(h func(*Conn, string))

SetPongHandler .

func (*Upgrader) Upgrade

func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header, args ...interface{}) (*Conn, error)

Upgrade .

func (*Upgrader) UpgradeAndTransferConnToPoller added in v1.3.16

func (u *Upgrader) UpgradeAndTransferConnToPoller(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error)

func (*Upgrader) UpgradeWithoutHandlingReadForConnFromSTDServer added in v1.3.19

func (u *Upgrader) UpgradeWithoutHandlingReadForConnFromSTDServer(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error)

Jump to

Keyboard shortcuts

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