ws

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2022 License: BlueOak-1.0.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// ErrPeerDisconnected will be returned if Send or Request is called on a
	// disconnected link.
	ErrPeerDisconnected = dex.ErrorKind("peer disconnected")

	ErrHandshake = dex.ErrorKind("handshake error")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection interface {
	Close() error

	SetReadDeadline(t time.Time) error
	ReadMessage() (int, []byte, error)
	SetReadLimit(limit int64)

	SetWriteDeadline(t time.Time) error
	WriteMessage(int, []byte) error
	WriteControl(messageType int, data []byte, deadline time.Time) error
}

Connection represents a websocket connection to a remote peer. In practice, it is satisfied by *websocket.Conn. For testing, a stub can be used.

func NewConnection

func NewConnection(w http.ResponseWriter, r *http.Request, readTimeout time.Duration) (Connection, error)

NewConnection attempts to to upgrade the http connection to a websocket Connection. If the upgrade fails, a reply will be sent with an appropriate error code.

type WSLink struct {
	// contains filtered or unexported fields
}

WSLink is the local, per-connection representation of a DEX peer (client or server) connection.

func NewWSLink(addr string, conn Connection, pingPeriod time.Duration, handler func(*msgjson.Message) *msgjson.Error, logger dex.Logger) *WSLink

NewWSLink is a constructor for a new WSLink.

func (*WSLink) Addr added in v0.2.0

func (c *WSLink) Addr() string

Addr returns the string-encoded IP address.

func (*WSLink) Connect

func (c *WSLink) Connect(ctx context.Context) (*sync.WaitGroup, error)

Connect begins processing input and output messages. Do not send messages until connected.

func (*WSLink) Disconnect

func (c *WSLink) Disconnect()

Disconnect begins shutdown of the WSLink, preventing new messages from entering the outgoing queue, and ultimately closing the underlying connection when all queued messages have been handled. This shutdown process is complete when the WaitGroup returned by Connect is Done.

func (*WSLink) Done

func (c *WSLink) Done() <-chan struct{}

Done returns a channel that is closed when the link goes down.

func (*WSLink) Off

func (c *WSLink) Off() bool

Off will return true if the link has disconnected.

func (*WSLink) Send

func (c *WSLink) Send(msg *msgjson.Message) error

Send sends the passed Message to the websocket peer. The actual writing of the message on the peer's link occurs asynchronously. As such, a nil error only indicates that the link is believed to be up and the message was successfully marshalled.

func (*WSLink) SendError

func (c *WSLink) SendError(id uint64, rpcErr *msgjson.Error)

SendError sends the msgjson.Error to the peer in a ResponsePayload.

func (*WSLink) SendNow

func (c *WSLink) SendNow(msg *msgjson.Message) error

SendNow is like send, but it waits for the message to be written on the peer's link, returning any error from the write.

func (*WSLink) SendRaw added in v0.5.0

func (c *WSLink) SendRaw(b []byte) error

SendRaw sends the passed bytes to the websocket peer. The actual writing of the message on the peer's link occurs asynchronously. As such, a nil error only indicates that the link is believed to be up.

func (*WSLink) SetReadLimit

func (c *WSLink) SetReadLimit(limit int64)

SetReadLimit should only be called before starting the Connection, or in a request or response handler that is run synchronously with other text or binary frame reads (e.g. ReadMessage).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL