websocket

package
v0.0.0-...-2cb4bbe Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConnectionNotEstablished is returned by methods that need a connection
	// but no connection is already created.
	ErrConnectionNotEstablished = errors.New("connection has not yet been established")
)

Functions

func HijackIfPossible

func HijackIfPossible(w http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error)

HijackIfPossible calls Hijack() on the given http.ResponseWriter if it implements http.Hijacker interface, which is required for net/http/httputil/reverseproxy to handle connection upgrade/switching protocol. Otherwise returns an error.

Types

type ManagedConnection

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

ManagedConnection represents a websocket connection.

func NewDurableConnection

func NewDurableConnection(target string, messageChan chan []byte, logger *zap.SugaredLogger) *ManagedConnection

NewDurableConnection creates a new websocket connection, that passes incoming messages to the given message channel. It can also send messages to the endpoint it connects to. The connection will continuously be kept alive and reconnected in case of a loss of connectivity.

Note: The given channel needs to be drained after calling `Shutdown` to not cause any deadlocks. If the channel's buffer is likely to be filled, this needs to happen in separate goroutines, i.e.

go func() {conn.Shutdown(); close(messageChan)} go func() {for range messageChan {}}

func NewDurableSendingConnection

func NewDurableSendingConnection(target string, logger *zap.SugaredLogger) *ManagedConnection

NewDurableSendingConnection creates a new websocket connection that can only send messages to the endpoint it connects to. The connection will continuously be kept alive and reconnected in case of a loss of connectivity.

func NewDurableSendingConnectionGuaranteed

func NewDurableSendingConnectionGuaranteed(target string, duration time.Duration, logger *zap.SugaredLogger) (*ManagedConnection, error)

NewDurableSendingConnectionGuaranteed creates a new websocket connection that can only send messages to the endpoint it connects to. It returns the connection if the connection can be established within the given `duration`. Otherwise it returns the ErrConnectionNotEstablished error.

The connection will continuously be kept alive and reconnected in case of a loss of connectivity.

func (*ManagedConnection) Send

func (c *ManagedConnection) Send(msg interface{}) error

Send sends an encodable message over the websocket connection.

func (*ManagedConnection) SendRaw

func (c *ManagedConnection) SendRaw(messageType int, msg []byte) error

SendRaw sends a message over the websocket connection without performing any encoding.

func (*ManagedConnection) Shutdown

func (c *ManagedConnection) Shutdown() error

Shutdown closes the websocket connection.

func (*ManagedConnection) Status

func (c *ManagedConnection) Status() error

Status checks the connection status of the webhook.

Jump to

Keyboard shortcuts

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