Documentation ¶
Overview ¶
Package ws implements websocket for advLayer.
Reference ¶
websocket rfc: https://datatracker.ietf.org/doc/html/rfc6455/
Below is a real websocket handshake progress:
Request
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 Origin: http://example.com
Response
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: chat
websocket packages comparison: https://yalantis.com/blog/how-to-build-websockets-in-go/
中文翻译: https://tonybai.com/2019/09/28/how-to-build-websockets-in-go/
All in all gobwas/ws is the best package. We use gobwas/ws.
gobwas包只支持http1.1, 所以如果使用nginx前置,确保 proxy_http_version 1.1;
Index ¶
- Constants
- type Client
- type Conn
- func (c *Conn) CanSpliceWrite() (r bool, conn *net.TCPConn)
- func (c *Conn) EverPossibleToSpliceWrite() bool
- func (c *Conn) Read(p []byte) (int, error)
- func (c *Conn) ReadFrom(r io.Reader) (written int64, err error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) Write(p []byte) (n int, e error)
- func (c *Conn) WriteBuffers(buffers [][]byte) (int64, error)
- type Creator
- func (Creator) CanHandleHeaders() bool
- func (Creator) GetDefaultAlpn() (alpn string, mustUse bool)
- func (Creator) IsMux() bool
- func (Creator) IsSuper() bool
- func (Creator) NewClientFromConf(conf *advLayer.Conf) (advLayer.Client, error)
- func (Creator) NewServerFromConf(conf *advLayer.Conf) (advLayer.Server, error)
- func (Creator) PackageID() string
- func (Creator) ProtocolName() string
- type EarlyDataConn
- type Server
Constants ¶
const MaxEarlyDataLen = 2048
const MaxEarlyDataLen_Base64 = 2732
2048 /3 = 682.6666... (682 又 三分之二), 683 * 4 = 2732, 你若不信,运行 we_test.go中的 TestBase64Len
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
implements advLayer.SingleClient
func NewClient ¶
func NewClient(hostAddr, path string, headers *httpLayer.HeaderPreset, isEarly bool) (*Client, error)
这里默认,传入的path必须 以 "/" 为前缀. 本函数 不对此进行任何检查
func (*Client) GetCreator ¶ added in v1.2.0
type Conn ¶
实现 net.Conn, io.ReaderFrom, utils.MultiWriter, netLayer.Splicer 因为 gobwas/ws 不包装conn,在写入和读取二进制时需要使用 较为底层的函数才行,并未被提供标准的Read和Write。 因此我们包装一下,统一使用Read和Write函数 来读写 二进制数据。因为我们这里是代理,
func (*Conn) CanSpliceWrite ¶ added in v1.2.5
func (*Conn) EverPossibleToSpliceWrite ¶ added in v1.2.5
func (*Conn) RemoteAddr ¶ added in v1.2.4
type Creator ¶
type Creator struct{}
func (Creator) CanHandleHeaders ¶ added in v1.2.0
func (Creator) GetDefaultAlpn ¶
func (Creator) NewClientFromConf ¶
func (Creator) NewServerFromConf ¶
func (Creator) ProtocolName ¶ added in v1.2.0
type EarlyDataConn ¶
type Server ¶
type Server struct { Creator UseEarlyData bool Thepath string RequestHeaders map[string][]string // contains filtered or unexported fields }
implements advLayer.SingleServer
func NewServer ¶
func NewServer(path string, headers *httpLayer.HeaderPreset, UseEarlyData bool) *Server
这里默认: 传入的path必须 以 "/" 为前缀. 本函数 不对此进行任何检查.