Back to godoc.org
github.com/cbron/pkg / websocket

Package websocket

v0.0.0-...-475a2bf
Latest Go to latest

The highest tagged major version is .

Published: May 14, 2020 | License: Apache-2.0 | Module: github.com/cbron/pkg

Index

Variables

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")
)

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.

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 (*ManagedConnection) Send

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

Send sends an encodable message over the websocket connection.

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.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier