Documentation
¶
Index ¶
- func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn
- type CloseError
- type Conn
- func (c *Conn) Close(code StatusCode, reason string) error
- func (c *Conn) CloseRead(ctx context.Context) context.Context
- func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error)
- func (c *Conn) Reader(ctx context.Context) (MessageType, io.Reader, error)
- func (c *Conn) SetReadLimit(n int64)
- func (c *Conn) Subprotocol() string
- func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error
- func (c *Conn) Writer(ctx context.Context, typ MessageType) (io.WriteCloser, error)
- type DialOptions
- type MessageType
- type StatusCode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NetConn ¶
NetConn converts a *websocket.Conn into a net.Conn.
It's for tunneling arbitrary protocols over WebSockets. Few users of the library will need this but it's tricky to implement correctly and so provided in the library. See https://github.com/nhooyr/websocket/issues/100.
Every Write to the net.Conn will correspond to a message write of the given type on *websocket.Conn.
The passed ctx bounds the lifetime of the net.Conn. If cancelled, all reads and writes on the net.Conn will be cancelled.
If a message is read that is not of the correct type, the connection will be closed with StatusUnsupportedData and an error will be returned.
Close will close the *websocket.Conn with StatusNormalClosure.
When a deadline is hit, the connection will be closed. This is different from most net.Conn implementations where only the reading/writing goroutines are interrupted but the connection is kept alive.
The Addr methods will return a mock net.Addr that returns "websocket" for Network and "websocket/unknown-addr" for String.
A received StatusNormalClosure or StatusGoingAway close frame will be translated to io.EOF when reading.
Types ¶
type CloseError ¶
type CloseError struct { Code StatusCode Reason string }
CloseError represents a WebSocket close frame. It is returned by Conn's methods when a WebSocket close frame is received from the peer. You will need to use the https://golang.org/pkg/errors/#As function, new in Go 1.13, to check for this error. See the CloseError example.
func (CloseError) Error ¶
func (ce CloseError) Error() string
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn provides a wrapper around the browser WebSocket API.
func Dial ¶
Dial creates a new WebSocket connection to the given url with the given options. The passed context bounds the maximum time spent waiting for the connection to open. The returned *http.Response is always nil or the zero value. It's only in the signature to match the core API.
func (*Conn) Close ¶
func (c *Conn) Close(code StatusCode, reason string) error
Close closes the websocket with the given code and reason. It will wait until the peer responds with a close frame or the connection is closed. It thus performs the full WebSocket close handshake.
func (*Conn) CloseRead ¶
CloseRead will start a goroutine to read from the connection until it is closed or a data message is received. If a data message is received, the connection will be closed with StatusPolicyViolation. Since CloseRead reads from the connection, it will respond to ping, pong and close frames. After calling this method, you cannot read any data messages from the connection. The returned context will be cancelled when the connection is closed.
Use this when you do not want to read data messages from the connection anymore but will want to write messages to it.
func (*Conn) Read ¶
Read attempts to read a message from the connection. The maximum time spent waiting is bounded by the context.
func (*Conn) Reader ¶
Reader attempts to read a message from the connection. The maximum time spent waiting is bounded by the context.
func (*Conn) SetReadLimit ¶
SetReadLimit sets the max number of bytes to read for a single message. It applies to the Reader and Read methods.
By default, the connection has a message read limit of 32768 bytes.
When the limit is hit, the connection will be closed with StatusMessageTooBig.
func (*Conn) Subprotocol ¶
Subprotocol returns the negotiated subprotocol. An empty string means the default protocol.
func (*Conn) Write ¶
Write writes a message of the given type to the connection. Always non blocking.
func (*Conn) Writer ¶
func (c *Conn) Writer(ctx context.Context, typ MessageType) (io.WriteCloser, error)
Writer returns a writer to write a WebSocket data message to the connection. It buffers the entire message in memory and then sends it when the writer is closed.
type DialOptions ¶
type DialOptions struct { // Subprotocols lists the subprotocols to negotiate with the server. Subprotocols []string }
DialOptions represents the options available to pass to Dial.
type MessageType ¶
type MessageType int
MessageType represents the type of a WebSocket message. See https://tools.ietf.org/html/rfc6455#section-5.6
const ( // MessageText is for UTF-8 encoded text messages like JSON. MessageText MessageType = iota + 1 // MessageBinary is for binary messages like Protobufs. MessageBinary )
MessageType constants.
func (MessageType) String ¶
func (i MessageType) String() string
type StatusCode ¶
type StatusCode int
StatusCode represents a WebSocket status code. https://tools.ietf.org/html/rfc6455#section-7.4
const ( StatusNormalClosure StatusCode = 1000 StatusGoingAway StatusCode = 1001 StatusProtocolError StatusCode = 1002 StatusUnsupportedData StatusCode = 1003 // StatusNoStatusRcvd cannot be sent in a close message. // It is reserved for when a close message is received without // an explicit status. StatusNoStatusRcvd StatusCode = 1005 // StatusAbnormalClosure is only exported for use with Wasm. // In non Wasm Go, the returned error will indicate whether the connection was closed or not or what happened. StatusAbnormalClosure StatusCode = 1006 StatusInvalidFramePayloadData StatusCode = 1007 StatusPolicyViolation StatusCode = 1008 StatusMessageTooBig StatusCode = 1009 StatusMandatoryExtension StatusCode = 1010 StatusInternalError StatusCode = 1011 StatusServiceRestart StatusCode = 1012 StatusTryAgainLater StatusCode = 1013 StatusBadGateway StatusCode = 1014 // StatusTLSHandshake is only exported for use with Wasm. // In non Wasm Go, the returned error will indicate whether there was a TLS handshake failure. StatusTLSHandshake StatusCode = 1015 )
These codes were retrieved from: https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
The defined constants only represent the status codes registered with IANA. The 4000-4999 range of status codes is reserved for arbitrary use by applications.
func CloseStatus ¶
func CloseStatus(err error) StatusCode
CloseStatus is a convenience wrapper around errors.As to grab the status code from a *CloseError. If the passed error is nil or not a *CloseError, the returned StatusCode will be -1.
func (StatusCode) String ¶
func (i StatusCode) String() string
Directories
¶
Path | Synopsis |
---|---|
internal
|
|
wsjs
Package wsjs implements typed access to the browser javascript WebSocket API.
|
Package wsjs implements typed access to the browser javascript WebSocket API. |
Package wsjson provides websocket helpers for JSON messages.
|
Package wsjson provides websocket helpers for JSON messages. |