Documentation ¶
Index ¶
- Variables
- func CloseCode(err error) int
- func CloseReason(err error) string
- type CloseError
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) CloseAndClean(err error)
- func (c *Conn) CloseWithError(err error)
- func (c *Conn) CompressionEnabled() bool
- func (c *Conn) EnableCompression(enable bool)
- func (c *Conn) EnableWriteCompression(enable bool)
- func (c *Conn) HandleRead(bufSize int)
- func (c *Conn) IsAsyncWrite() bool
- func (c *Conn) IsBlockingMod() bool
- func (c *Conn) IsClient() bool
- func (c *Conn) Keepalive(d time.Duration) *time.Timer
- func (c *Conn) OnClose(h func(*Conn, error))
- func (c *Conn) OnDataFrame(h func(*Conn, MessageType, bool, []byte))
- func (c *Conn) OnMessage(h func(*Conn, MessageType, []byte))
- func (c *Conn) Parse(data []byte) error
- func (c *Conn) Session() interface{}
- func (c *Conn) SessionWithContext(ctx context.Context) interface{}
- func (c *Conn) SessionWithLock() interface{}
- func (c *Conn) SetClient(isClient bool)
- func (c *Conn) SetCloseError(err error)
- func (c *Conn) SetSession(session interface{})
- func (c *Conn) Subprotocol() string
- func (c *Conn) UnderlayerConn() net.Conn
- func (c *Conn) Write(data []byte) (int, error)
- func (c *Conn) WriteClose(code int, reason string) error
- func (c *Conn) WriteFrame(messageType MessageType, sendOpcode, fin bool, data []byte) error
- func (c *Conn) WriteMessage(messageType MessageType, data []byte) error
- type Dialer
- type MessageType
- type Options
- type Upgrader
- func (u *Upgrader) EnableCompression(enable bool)
- func (u *Upgrader) OnClose(h func(*Conn, error))
- func (u *Upgrader) OnDataFrame(h func(*Conn, MessageType, bool, []byte))
- func (u *Upgrader) OnMessage(h func(*Conn, MessageType, []byte))
- func (u *Upgrader) OnOpen(h func(*Conn))
- func (u *Upgrader) SetCloseHandler(h func(*Conn, int, string))
- func (u *Upgrader) SetCompressionLevel(level int) error
- func (u *Upgrader) SetPingHandler(h func(*Conn, string))
- func (u *Upgrader) SetPongHandler(h func(*Conn, string))
- func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header, ...) (*Conn, error)
- func (u *Upgrader) UpgradeAndTransferConnToPoller(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error)
- func (u *Upgrader) UpgradeWithoutHandlingReadForConnFromSTDServer(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error)
Constants ¶
This section is empty.
Variables ¶
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") )
var ( // DefaultBlockingReadBufferSize . DefaultBlockingReadBufferSize = 1024 * 4 // DefaultBlockingModAsyncWrite . DefaultBlockingModAsyncWrite = true // DefaultBlockingModHandleRead . DefaultBlockingModHandleRead = true // DefaultBlockingModTransferConnToPoller . DefaultBlockingModTransferConnToPoller = false // DefaultBlockingModSendQueueInitSize . DefaultBlockingModSendQueueInitSize = 4 // DefaultBlockingModSendQueueMaxSize . DefaultBlockingModSendQueueMaxSize uint16 = 0 // DefaultMessageLengthLimit . DefaultMessageLengthLimit = 1024 * 1024 * 4 // DefaultBlockingModAsyncCloseDelay . 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 ¶
Types ¶
type CloseError ¶ added in v1.3.17
CloseError .
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 NewServerConn ¶ added in v1.4.0
func (*Conn) CloseAndClean ¶ added in v1.3.16
CloseAndClean .
func (*Conn) CloseWithError ¶ added in v1.3.17
CloseWithError .
func (*Conn) CompressionEnabled ¶ added in v1.3.16
CompressionEnabled .
func (*Conn) EnableCompression ¶ added in v1.3.16
EnableCompression .
func (*Conn) EnableWriteCompression ¶ added in v1.1.2
EnableWriteCompression .
func (*Conn) OnDataFrame ¶ added in v1.1.2
func (c *Conn) OnDataFrame(h func(*Conn, MessageType, bool, []byte))
OnDataFrame .
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
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) SetCloseError ¶ added in v1.3.17
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
Subprotocol returns the negotiated websocket subprotocol.
func (*Conn) UnderlayerConn ¶ added in v1.4.0
func (*Conn) WriteClose ¶ added in v1.3.17
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 .
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 (*Upgrader) EnableCompression ¶ added in v1.1.2
EnableCompression .
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) SetCloseHandler ¶ added in v1.1.22
SetCloseHandler .
func (*Upgrader) SetCompressionLevel ¶ added in v1.1.22
SetCompressionLevel .
func (*Upgrader) SetPingHandler ¶ added in v1.1.22
SetPingHandler .
func (*Upgrader) SetPongHandler ¶ added in v1.1.22
SetPongHandler .
func (*Upgrader) Upgrade ¶
func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header, args ...interface{}) (*Conn, error)
Upgrade .