websocket

package
v1.3.13 Latest Latest
Warning

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

Go to latest
Published: May 10, 2023 License: MIT Imports: 23 Imported by: 0

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")

	// ErrReservedOpcodeSet .
	ErrReservedOpcodeSet = errors.New("websocket: reserved opcode received")

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

	// ErrFragmentsShouldNotHaveBinaryOrTextOpcode .
	ErrFragmentsShouldNotHaveBinaryOrTextOpcode = errors.New("websocket: fragments should not have opcode 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")

	// ErrMalformedURL .
	ErrMalformedURL = errors.New("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 represents whether create a goroutine to handle writing:
	// true : create a goroutine to recv buffers and write to conn, default is true;
	// false: write buffer to the conn directely.
	DefaultBlockingModAsyncWrite = false

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

Functions

This section is empty.

Types

type Conn

type Conn struct {
	net.Conn

	Engine *nbhttp.Engine
	// contains filtered or unexported fields
}

Conn .

func NewConn

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

func (*Conn) EnableWriteCompression

func (c *Conn) EnableWriteCompression(enable bool)

EnableWriteCompression .

func (*Conn) OnClose

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

OnClose .

func (*Conn) Session

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

Session returns user session.

func (*Conn) SetCompressionLevel

func (c *Conn) SetCompressionLevel(level int) error

SetCompressionLevel .

func (*Conn) SetSession

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

SetSession sets user session.

func (*Conn) Subprotocol

func (c *Conn) Subprotocol() string

Subprotocol returns the negotiated websocket subprotocol.

func (*Conn) Write

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

Write overwrites nbio.Conn.Write.

func (*Conn) WriteFrame

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

type Dialer struct {
	Engine *nbhttp.Engine

	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

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

Dial .

func (*Dialer) DialContext

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

DialContext .

type MessageType

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 Upgrader

type Upgrader = WebsocketReader

Upgrader .

func NewUpgrader

func NewUpgrader() *Upgrader

NewUpgrader .

type WebsocketReader

type WebsocketReader struct {
	ReadLimit int64
	// MessageLengthLimit is the maximum length of websocket message. 0 for unlimited.
	MessageLengthLimit int64
	HandshakeTimeout   time.Duration
	KeepaliveTime      time.Duration

	Subprotocols []string

	CheckOrigin func(r *http.Request) bool

	Engine *nbhttp.Engine

	BlockingModReadBufferSize int
	BlockingModAsyncWrite     bool
	// contains filtered or unexported fields
}

func NewWebsocketReader

func NewWebsocketReader() *WebsocketReader

NewWebsocketReader .

func (*WebsocketReader) BlockingModReadLoop

func (wr *WebsocketReader) BlockingModReadLoop()

BlockingModReadLoop .

func (*WebsocketReader) BlockingModWriteLoop

func (wr *WebsocketReader) BlockingModWriteLoop()

BlockingModWriteLoop .

func (*WebsocketReader) Close

func (wr *WebsocketReader) Close(p *nbhttp.Parser, err error)

Close .

func (*WebsocketReader) CompressionEnabled

func (wr *WebsocketReader) CompressionEnabled() bool

CompressionEnabled .

func (*WebsocketReader) EnableCompression

func (wr *WebsocketReader) EnableCompression(enable bool)

EnableCompression .

func (*WebsocketReader) EnableWriteCompression

func (wr *WebsocketReader) EnableWriteCompression(enable bool)

EnableWriteCompression .

func (*WebsocketReader) OnClose

func (wr *WebsocketReader) OnClose(h func(*Conn, error))

OnClose .

func (*WebsocketReader) OnDataFrame

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

OnDataFrame .

func (*WebsocketReader) OnMessage

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

OnMessage .

func (*WebsocketReader) OnOpen

func (wr *WebsocketReader) OnOpen(h func(*Conn))

OnOpen .

func (*WebsocketReader) Read

func (wr *WebsocketReader) Read(p *nbhttp.Parser, data []byte) error

Read .

func (*WebsocketReader) SetBlockingMod

func (wr *WebsocketReader) SetBlockingMod(blocking bool)

SetBlockingMod .

func (*WebsocketReader) SetCloseHandler

func (wr *WebsocketReader) SetCloseHandler(h func(*Conn, int, string))

SetCloseHandler .

func (*WebsocketReader) SetCompressionLevel

func (wr *WebsocketReader) SetCompressionLevel(level int) error

SetCompressionLevel .

func (*WebsocketReader) SetConn

func (wr *WebsocketReader) SetConn(conn *Conn)

SetConn .

func (*WebsocketReader) SetPingHandler

func (wr *WebsocketReader) SetPingHandler(h func(*Conn, string))

SetPingHandler .

func (*WebsocketReader) SetPongHandler

func (wr *WebsocketReader) SetPongHandler(h func(*Conn, string))

SetPongHandler .

func (*WebsocketReader) Upgrade

func (wr *WebsocketReader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (net.Conn, error)

Upgrade .

Jump to

Keyboard shortcuts

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