Documentation
¶
Overview ¶
Copyright 2021-2023 antlabs. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2021-2023 antlabs. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2021-2023 antlabs. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- Constants
- Variables
- func StringToBytes(s string) (b []byte)
- type Callback
- type ClientOption
- func WithClientBufioMultipleTimesPayloadSize(mt float32) ClientOption
- func WithClientBufioParseMode() ClientOption
- func WithClientCallback(cb Callback) ClientOption
- func WithClientCompression() ClientOption
- func WithClientDecompressAndCompress() ClientOption
- func WithClientDecompression() ClientOption
- func WithClientDialTimeout(t time.Duration) ClientOption
- func WithClientDisableBufioClearHack() ClientOption
- func WithClientEnableUTF8Check() ClientOption
- func WithClientHTTPHeader(h http.Header) ClientOption
- func WithClientIgnorePong() ClientOption
- func WithClientOnMessageFunc(cb OnMessageFunc) ClientOption
- func WithClientReplyPing() ClientOption
- func WithClientTCPDelay() ClientOption
- func WithClientTLSConfig(tls *tls.Config) ClientOption
- func WithClientWindowsMultipleTimesPayloadSize(mt float32) ClientOption
- func WithClientWindowsParseMode() ClientOption
- type CloseErrMsg
- type Config
- type Conn
- func (c *Conn) Close() (err error)
- func (c *Conn) ReadLoop() error
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) StartReadLoop()
- func (c *Conn) WriteCloseTimeout(sc StatusCode, t time.Duration) (err error)
- func (c *Conn) WriteControl(op Opcode, data []byte) (err error)
- func (c *Conn) WriteMessage(op Opcode, writeBuf []byte) (err error)
- func (c *Conn) WriteMessageDelay(op Opcode, writeBuf []byte) (err error)
- func (c *Conn) WritePing(data []byte) (err error)
- func (c *Conn) WritePong(data []byte) (err error)
- func (c *Conn) WriteTimeout(op Opcode, data []byte, t time.Duration) (err error)
- type ConnOption
- type DefCallback
- type DialOption
- type OnMessageFunc
- type Opcode
- type ServerOption
- func WithServerBufioMultipleTimesPayloadSize(mt float32) ServerOption
- func WithServerBufioParseMode() ServerOption
- func WithServerCallback(cb Callback) ServerOption
- func WithServerDecompressAndCompress() ServerOption
- func WithServerDecompression() ServerOption
- func WithServerDelayWriteInitBufferSize(n int32) ServerOption
- func WithServerDisableBufioClearHack() ServerOption
- func WithServerEnableUTF8Check() ServerOption
- func WithServerIgnorePong() ServerOption
- func WithServerMaxDelayWriteDuration(d time.Duration) ServerOption
- func WithServerMaxDelayWriteNum(n int32) ServerOption
- func WithServerOnMessageFunc(cb OnMessageFunc) ServerOption
- func WithServerReadTimeout(t time.Duration) ServerOption
- func WithServerReplyPing() ServerOption
- func WithServerTCPDelay() ServerOption
- func WithServerWindowsMultipleTimesPayloadSize(mt float32) ServerOption
- func WithServerWindowsParseMode() ServerOption
- type StatusCode
- type UpgradeServer
Constants ¶
const ( Continuation = opcode.Continuation Text = opcode.Text Binary = opcode.Binary Close = opcode.Close Ping = opcode.Ping Pong = opcode.Pong )
const ( ParseModeBufio parseMode = iota ParseModeWindows )
Variables ¶
var ( // conn已经被关闭 ErrClosed = errors.New("closed") ErrWrongStatusCode = errors.New("Wrong status code") ErrUpgradeFieldValue = errors.New("The value of the upgrade field is not 'websocket'") ErrConnectionFieldValue = errors.New("The value of the connection field is not 'upgrade'") ErrSecWebSocketAccept = errors.New("The value of Sec-WebSocketAaccept field is invalid") ErrHostCannotBeEmpty = errors.New("Host cannot be empty") ErrSecWebSocketKey = errors.New("The value of SEC websocket key field is wrong") ErrSecWebSocketVersion = errors.New("The value of SEC websocket version field is wrong, not 13") ErrHTTPProtocolNotSupported = errors.New("HTTP protocol not supported") ErrOnlyGETSupported = errors.New("error:Only get methods are supported") ErrMaxControlFrameSize = errors.New("error:max control frame size > 125, need <= 125") ErrRsv123 = errors.New("error:rsv1 or rsv2 or rsv3 has a value") ErrOpcode = errors.New("error:wrong opcode") ErrNOTBeFragmented = errors.New("error:since control message MUST NOT be fragmented") ErrFrameOpcode = errors.New("error:since all data frames after the initial data frame must have opcode 0.") ErrTextNotUTF8 = errors.New("error:text is not utf8 data") ErrClosePayloadTooSmall = errors.New("error:close payload too small") ErrCloseValue = errors.New("error:close value is wrong") // close值不对 ErrEmptyClose = errors.New("error:close value is empty") // close的值是空的 ErrWriteClosed = errors.New("write close") )
var ErrClose = "websocket"
var (
ErrNotFoundHijacker = errors.New("not found Hijacker")
)
Functions ¶
Types ¶
type ClientOption ¶
type ClientOption func(*DialOption)
func WithClientBufioMultipleTimesPayloadSize ¶ added in v0.0.11
func WithClientBufioMultipleTimesPayloadSize(mt float32) ClientOption
func WithClientBufioParseMode ¶ added in v0.0.9
func WithClientBufioParseMode() ClientOption
func WithClientCallback ¶
func WithClientCallback(cb Callback) ClientOption
1. callback 配置客户端callback
func WithClientDisableBufioClearHack ¶ added in v0.0.9
func WithClientDisableBufioClearHack() ClientOption
func WithClientEnableUTF8Check ¶ added in v0.0.9
func WithClientEnableUTF8Check() ClientOption
func WithClientOnMessageFunc ¶
func WithClientOnMessageFunc(cb OnMessageFunc) ClientOption
仅仅配置OnMessae函数
func WithClientReplyPing ¶
func WithClientReplyPing() ClientOption
配置自动回应ping frame, 当收到ping, 回一个pong
func WithClientWindowsMultipleTimesPayloadSize ¶ added in v0.0.9
func WithClientWindowsMultipleTimesPayloadSize(mt float32) ClientOption
func WithClientWindowsParseMode ¶ added in v0.0.9
func WithClientWindowsParseMode() ClientOption
默认使用窗口解析方式, 以后以后默认解析方式改变过,才有必要使用这个选项
type CloseErrMsg ¶
type CloseErrMsg struct { Code StatusCode Msg string }
func (CloseErrMsg) Error ¶
func (c CloseErrMsg) Error() string
type Config ¶
type Config struct { Callback // contains filtered or unexported fields }
func ClientOptionToConf ¶ added in v0.0.9
func ClientOptionToConf(opts ...ClientOption) *Config
type Conn ¶
type Conn struct { *Config // contains filtered or unexported fields }
func Dial ¶
func Dial(rawUrl string, opts ...ClientOption) (*Conn, error)
https://datatracker.ietf.org/doc/html/rfc6455#section-4.1 又是一顿if else, 咬文嚼字
func Upgrade ¶
func Upgrade(w http.ResponseWriter, r *http.Request, opts ...ServerOption) (c *Conn, err error)
func (*Conn) StartReadLoop ¶
func (c *Conn) StartReadLoop()
func (*Conn) WriteCloseTimeout ¶
func (c *Conn) WriteCloseTimeout(sc StatusCode, t time.Duration) (err error)
func (*Conn) WriteControl ¶ added in v0.0.9
func (*Conn) WriteMessageDelay ¶ added in v0.0.12
延迟写消息 1. 如果缓存的消息超过了多少条数 2. 如果缓存的消费超过了多久的时间
type ConnOption ¶
type ConnOption struct {
Config
}
type DefCallback ¶
type DefCallback struct{}
func (*DefCallback) OnClose ¶
func (defcallback *DefCallback) OnClose(_ *Conn, _ error)
func (*DefCallback) OnMessage ¶
func (defcallback *DefCallback) OnMessage(_ *Conn, _ Opcode, _ []byte)
func (*DefCallback) OnOpen ¶
func (defcallback *DefCallback) OnOpen(_ *Conn)
type DialOption ¶
func (*DialOption) Dial ¶
func (d *DialOption) Dial() (c *Conn, err error)
type OnMessageFunc ¶
func (OnMessageFunc) OnClose ¶
func (o OnMessageFunc) OnClose(_ *Conn, _ error)
func (OnMessageFunc) OnOpen ¶
func (o OnMessageFunc) OnOpen(_ *Conn)
type ServerOption ¶
type ServerOption func(*ConnOption)
func WithServerBufioMultipleTimesPayloadSize ¶ added in v0.0.11
func WithServerBufioMultipleTimesPayloadSize(mt float32) ServerOption
12 配置多倍payload缓冲区, 1.是1024 2。是2048 为何不让用户自己配置呢,可以和底层的buffer池结合起来,/1024就知道命中哪个缓冲区了, 不需要维护index命中的哪个sync.Pool 如果用户传些奇奇怪怪的数字,就不好办了
func WithServerBufioParseMode ¶ added in v0.0.9
func WithServerBufioParseMode() ServerOption
9.
使用基于bufio的解析方式
func WithServerDecompressAndCompress ¶
func WithServerDecompressAndCompress() ServerOption
2.配置压缩和解压缩
func WithServerDecompression ¶
func WithServerDecompression() ServerOption
func WithServerDelayWriteInitBufferSize ¶ added in v0.0.12
func WithServerDelayWriteInitBufferSize(n int32) ServerOption
15. 配置延迟包的初始化buffer大小
func WithServerDisableBufioClearHack ¶ added in v0.0.9
func WithServerDisableBufioClearHack() ServerOption
11 关闭bufio clear hack优化
func WithServerEnableUTF8Check ¶ added in v0.0.9
func WithServerEnableUTF8Check() ServerOption
3.关闭utf8检查
func WithServerIgnorePong ¶
func WithServerIgnorePong() ServerOption
func WithServerMaxDelayWriteDuration ¶ added in v0.0.12
func WithServerMaxDelayWriteDuration(d time.Duration) ServerOption
13. 配置延迟发送 配置延迟最大发送时间
func WithServerMaxDelayWriteNum ¶ added in v0.0.12
func WithServerMaxDelayWriteNum(n int32) ServerOption
14. 配置最大延迟个数
func WithServerOnMessageFunc ¶
func WithServerOnMessageFunc(cb OnMessageFunc) ServerOption
4.仅仅配置OnMessae函数 仅仅配置OnMessae函数
func WithServerReplyPing ¶
func WithServerReplyPing() ServerOption
5. 配置自动回应ping frame, 当收到ping, 回一个pong
func WithServerTCPDelay ¶
func WithServerTCPDelay() ServerOption
设置TCP_NODELAY 为false, 开启nagle算法 设置服务端TCP_NODELAY
func WithServerWindowsMultipleTimesPayloadSize ¶ added in v0.0.9
func WithServerWindowsMultipleTimesPayloadSize(mt float32) ServerOption
7. 设置几倍payload的缓冲区 只有解析方式是窗口的时候才有效 如果为1.0就是1024 + 14, 如果是2.0就是2048 + 14
func WithServerWindowsParseMode ¶ added in v0.0.9
func WithServerWindowsParseMode() ServerOption
8 配置windows解析方式 默认使用窗口解析方式, 以后以后默认解析方式改变过,才有必要使用这个选项
type StatusCode ¶
type StatusCode int16
https://datatracker.ietf.org/doc/html/rfc6455#section-7.4.1 这里记录了各种状态码的含义
const ( // NormalClosure 正常关闭 NormalClosure StatusCode = 1000 // EndpointGoingAway 对端正在消失 EndpointGoingAway StatusCode = 1001 // ProtocolError 表示对端由于协议错误正在终止连接 ProtocolError StatusCode = 1002 // DataCannotAccept 收到一个不能接受的数据类型 DataCannotAccept StatusCode = 1003 // NotConsistentMessageType 表示对端正在终止连接, 消息类型不一致 NotConsistentMessageType StatusCode = 1007 // TerminatingConnection 表示对端正在终止连接, 没有好用的错误, 可以用这个错误码表示 TerminatingConnection StatusCode = 1008 // TooBigMessage 消息太大, 不能处理, 关闭连接 TooBigMessage StatusCode = 1009 // NoExtensions 只用于客户端, 服务端返回扩展消息 NoExtensions StatusCode = 1010 // ServerTerminating 服务端遇到意外情况, 中止请求 ServerTerminating StatusCode = 1011 )
func (StatusCode) String ¶
func (s StatusCode) String() string
type UpgradeServer ¶
type UpgradeServer struct {
// contains filtered or unexported fields
}
func NewUpgrade ¶
func NewUpgrade(opts ...ServerOption) *UpgradeServer
func (*UpgradeServer) Upgrade ¶
func (u *UpgradeServer) Upgrade(w http.ResponseWriter, r *http.Request) (c *Conn, err error)