ws

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxMessageSize = 1024 * 1024
)

Variables

View Source
var (
	ErrConnectionClosed = errors.New("connection closed")
)

Functions

This section is empty.

Types

type Connection added in v0.2.0

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

func New added in v0.6.0

func New(wsURL string, opts Options) (*Connection, error)

New initializes a new WebSocket connection configuration with specified URL and options. It takes wsURL, a string representing the WebSocket URL, and opts, an instance of Options with custom settings. It returns a pointer to a Connection and possible error if the URL is empty, poorly formatted, or headers are invalid.

func (*Connection) Close added in v0.2.0

func (c *Connection) Close() error

Close shuts down an established WebSocket connection gracefully. It returns an error if the connection is not yet established. The function ensures a normal closure status is sent to the WebSocket server.

func (*Connection) Connect added in v0.6.0

func (c *Connection) Connect(ctx context.Context) error

Connect establishes a WebSocket connection using the specified context. It returns an error if the onMessage callback is not set, the connection attempt fails, or if a connection is already established. The method locks the connection during setup to ensure thread safety and sets a default read limit on the WebSocket.

func (*Connection) Hostname added in v0.3.0

func (c *Connection) Hostname() string

Hostname retrieves the host name part of the URL stored in the Connection struct. It returns a string representing the host name.

func (*Connection) Ready added in v0.6.0

func (c *Connection) Ready() <-chan struct{}

Ready returns a channel that is closed when the WebSocket connection is established.

func (*Connection) Send added in v0.2.0

func (c *Connection) Send(ctx context.Context, msg string) error

Send transmits a message over an established WebSocket connection within a given context. It takes ctx of type context.Context and msg of type string as parameters. It returns an error if the context is canceled or if there is a failure writing to the WebSocket. The function waits for the connection to be ready before sending the message.

func (*Connection) SetOnMessage added in v0.6.0

func (c *Connection) SetOnMessage(onMessage func(context.Context, []byte))

SetOnMessage sets the callback function to handle incoming messages on the connection. It takes onMessage, a function with parameters context.Context and a byte slice [], as input. The method does not return any value and is thread-safe, locking access to the callback function.

type Mockreader added in v0.6.0

type Mockreader struct {
	mock.Mock
}

Mockreader is an autogenerated mock type for the reader type

func NewMockreader added in v0.6.0

func NewMockreader(t interface {
	mock.TestingT
	Cleanup(func())
}) *Mockreader

NewMockreader creates a new instance of Mockreader. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*Mockreader) EXPECT added in v0.6.0

func (_m *Mockreader) EXPECT() *Mockreader_Expecter

func (*Mockreader) Read added in v0.6.0

func (_m *Mockreader) Read(p []byte) (int, error)

Read provides a mock function with given fields: p

type Mockreader_Expecter added in v0.6.0

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

func (*Mockreader_Expecter) Read added in v0.6.0

func (_e *Mockreader_Expecter) Read(p interface{}) *Mockreader_Read_Call

Read is a helper method to define mock.On call

  • p []byte

type Mockreader_Read_Call added in v0.6.0

type Mockreader_Read_Call struct {
	*mock.Call
}

Mockreader_Read_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Read'

func (*Mockreader_Read_Call) Return added in v0.6.0

func (_c *Mockreader_Read_Call) Return(n int, err error) *Mockreader_Read_Call

func (*Mockreader_Read_Call) Run added in v0.6.0

func (_c *Mockreader_Read_Call) Run(run func(p []byte)) *Mockreader_Read_Call

func (*Mockreader_Read_Call) RunAndReturn added in v0.6.0

func (_c *Mockreader_Read_Call) RunAndReturn(run func([]byte) (int, error)) *Mockreader_Read_Call

type Options added in v0.2.0

type Options struct {
	Output              io.Writer
	Headers             []string
	SkipSSLVerification bool
	MaxMessageSize      int64
}

Jump to

Keyboard shortcuts

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