Documentation
¶
Overview ¶
Package coder is the neffos backend for github.com/coder/websocket.
Use Upgrader or DefaultUpgrader on the server (neffos.New) and Dialer or DefaultDialer on the client (neffos.Dial). Socket implements neffos.Socket and the optional neffos.SocketCloser, neffos.SocketPinger and neffos.SocketReadLimiter, so close codes, the heartbeat (WithTimeout.PingInterval) and WithTimeout.MaxMessageSize all work.
Defaults that differ from coder's own, or that are easy to miss:
- No read limit. coder caps messages at 32 KB by default; the socket turns that off, so a message of any size is read unless MaxMessageSize is set, the same as the gorilla and gobwas backends.
- Same-origin requests only. Accept rejects a request whose Origin header names another host with 403, as gorilla's default upgrader does. Set AcceptOptions.OriginPatterns to allow other origins.
- On the client side, NetConn().RemoteAddr() and LocalAddr() return coder's placeholder address, not the real one, because coder does not expose the dialed connection.
- NetConn().Close() closes the connection at once with no close frame. Use Socket.Close, or neffos.Conn.Close, to send one.
- A read or write timeout closes the connection. That is how coder handles an expired context, and neffos closes the connection after a timeout anyway.
Index ¶
- Variables
- func Dialer(opts *websocket.DialOptions) neffos.Dialer
- func Upgrader(opts websocket.AcceptOptions) neffos.Upgrader
- type Options
- type Socket
- func (s *Socket) Close(code int, reason string, timeout time.Duration) error
- func (s *Socket) NetConn() net.Conn
- func (s *Socket) Ping(timeout time.Duration) error
- func (s *Socket) ReadData(timeout time.Duration) ([]byte, neffos.MessageType, error)
- func (s *Socket) Request() *http.Request
- func (s *Socket) SetReadLimit(n int64)
- func (s *Socket) WriteBinary(body []byte, timeout time.Duration) error
- func (s *Socket) WriteText(body []byte, timeout time.Duration) error
Constants ¶
This section is empty.
Variables ¶
var DefaultDialer = Dialer(nil)
DefaultDialer is a coder/websocket dialer with all options left at their zero values.
var DefaultUpgrader = Upgrader(websocket.AcceptOptions{})
DefaultUpgrader is a coder/websocket upgrader with all options left at their zero values. It accepts same-origin requests only.
Functions ¶
func Dialer ¶
func Dialer(opts *websocket.DialOptions) neffos.Dialer
Dialer is a `neffos.Dialer` type for the coder/websocket implementation. Use it on `neffos.Dial` to create a client connection. opts may be nil.
The options go to websocket.Dial as they are. A set OnPongReceived still runs; the socket chains its own pong handling after it.
func Upgrader ¶
func Upgrader(opts websocket.AcceptOptions) neffos.Upgrader
Upgrader is a `neffos.Upgrader` type for the coder/websocket implementation. Use it on `neffos.New` to construct the neffos server.
The options go to websocket.Accept as they are, so OriginPatterns, Subprotocols, CompressionMode and InsecureSkipVerify work as coder documents them. A set OnPongReceived still runs; the socket chains its own pong handling after it.
Types ¶
type Options ¶
type Options = websocket.DialOptions
Options is the coder/websocket dial options type, so callers of Dialer do not need to import coder/websocket for it.
type Socket ¶
Socket completes the `neffos.Socket` interface, it describes the underline websocket connection.
It also implements neffos.SocketCloser, neffos.SocketPinger and neffos.SocketReadLimiter. coder/websocket serialises writes itself, so the socket adds no lock around them, and coder writes nothing after its close frame.
func (*Socket) Close ¶
Close sends a close frame with code and reason and waits up to timeout (one second when timeout is zero) for the remote side to answer, then closes the connection. It returns nil when the remote side answered with a close frame, or when it did not answer in time. Every later write fails. It is safe to call while other goroutines read and write.
When the remote side does not answer, Close still returns after timeout, but the transport can stay open in the background for up to coder's fixed 5s when no ReadData holds the read lock. That happens in two cases: no ReadData is running at all, or Close ran between two reads. In the second case coder's close handshake takes the read lock first, so the next ReadData fails with "failed to acquire lock: context canceled" and does not close the transport itself.
func (*Socket) NetConn ¶
NetConn returns the connection as a net.Conn, made with websocket.NetConn on first use. Its Close closes the connection at once, with no close handshake. On the client side its RemoteAddr and LocalAddr are coder's placeholder address, since coder does not expose the dialed net.Conn.
func (*Socket) Ping ¶
Ping sends a ping frame. With a timeout above zero it then waits up to that long for the pong and returns an error that matches os.ErrDeadlineExceeded when none arrives. With a zero timeout it returns at once and the ping goes out in the background. Pongs are only seen while another goroutine is inside ReadData. Ping(0) leaves a goroutine running for up to a second, because coder's Ping always waits for the pong.
func (*Socket) ReadData ¶
ReadData reads binary or text messages from the remote connection.
With a timeout above zero and no message in time, the connection is closed and the error matches os.ErrDeadlineExceeded. A pong from the remote side restarts that timeout. A close frame from the remote side is returned as a neffos.CloseError with its code and reason, and a connection that ends without one as a neffos.CloseError with neffos.CloseAbnormalClosure. A message over the read limit returns an error that matches neffos.ErrMessageTooBig, after coder sent a close frame with neffos.CloseMessageTooBig and the remote side answered it, or a second passed. That close handshake runs inside the goroutine that called ReadData, so against a remote side that never answers, ReadData takes about a second longer to return.
func (*Socket) SetReadLimit ¶
SetReadLimit sets the largest message, in bytes, ReadData accepts. A bigger message closes the connection with neffos.CloseMessageTooBig. n <= 0 removes the limit. It applies from the next message on.
func (*Socket) WriteBinary ¶
WriteBinary sends a binary message to the remote connection. With a timeout above zero, a write that takes longer closes the connection.