websocket

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

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

Message is the data produced by the websocket.

func (Message) Data

func (m Message) Data() []byte

Data returns the data stored in a Message. The byte slice should not be used after Release is called on a message.

func (Message) Release

func (m Message) Release()

Release the message back to the WebSocket's buffer pool. You should call this method after you are finished reading the message's data. Once called, the message's data is no longer valid, and should not be used.

type Options

type Options struct {
	PingInterval time.Duration

	ResetInterval time.Duration

	// PoolSize defines the size of the pool of pre-allocated buffers for storing data
	// received from the websocket connection. If not set, it will be set to 32 by
	// default.
	PoolSize int
	// BufCapacity sets the initial capacity of buffers in the websocket's buffer pool.
	// You should set this to be larger than the typical size of a message from the
	// specific websocket connection to reduce the number of memory allocations. If not
	// set, it will be set to 2048 bytes by default.
	BufCapacity int
}

type Websocket

type Websocket struct {
	Url string

	OnConnect     func() error
	ResetInterval time.Duration
	PingInterval  time.Duration
	// contains filtered or unexported fields
}

Websocket handles a websocket client connection to a given URL.

func New

func New(url string, opts *Options) Websocket

func (*Websocket) Close

func (ws *Websocket) Close()

Close sends a closes the websocket connection. The Messages channel will be closed immediately after.

func (*Websocket) Err

func (ws *Websocket) Err() <-chan error

func (*Websocket) Messages

func (ws *Websocket) Messages() <-chan Message

Messages returns a channel containing the messages received from the websocket. Each Message received should be released back to the websocket's buffer pool by calling Release once you are finished with the message.

func (*Websocket) Send

func (ws *Websocket) Send(data []byte)

Send a text message along the websocket.

func (*Websocket) Start

func (ws *Websocket) Start(ctx context.Context) error

Start the websocket connection. This function creates the websocket connection and immediately begins reading messages sent by the server. Start must be called before any messages can be received by the consumer.

Jump to

Keyboard shortcuts

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