rewebsocket

package module
v0.0.0-...-5f48e00 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2016 License: MIT Imports: 5 Imported by: 0

README

WebSocket client that automatically reconnects, based on Gorilla WebSocket.

Documentation

Overview

Package rewebsocket adds autoreconnect to Gorilla WebSocket.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialer

type Dialer func(cancel chan struct{}) (*websocket.Conn, error)

Dialer will be called to establish the WebSocket connection. The operation might will be retried using the Retry function. If this function blocks it will halt (re)connect and close progress. The given cancel channel will be closed when the TextClient is closed, allowing the user to cancel long running operations.

type TextClient

type TextClient struct {
	// OnReadMessage will be called for each incoming message. Messages will not
	// be processed concurrently unless the implementing function runs its logic
	// in a different goroutine. If this function blocks it will block the read
	// loop (which allows for flow control) and delay closing until it is
	// unblocked. If not set, messages will still be read but ignored. The given
	// cancel channel will be closed when the TextClient is closed,
	// allowing the user to cancel long running operations.
	OnReadMessage func(cancel chan struct{}, msg []byte)

	// OnError is called when there is a non-fatal error (typically failing to
	// read a message) This function will run in its own goroutine. If not set
	// the event will be lost.
	OnError func(error)

	// OnFatal is called when there is a fatal error (we can't reconnect after
	// retrying using the retry function). When there is a fatal error the client
	// will be closed automatically, right before calling OnFatal. If not set the
	// client will still automatically close but the event will be lost. This
	// function will run in its own goroutine.
	OnFatal func(error)

	// Retry is a function that retries the given function until it gives up and
	// returns an error when the given channel is closed. It is used when
	// reconnecting. When the function returns an error, OnFatal will be called
	// and the client will be closed automatically. If not set reconnect
	// operations will only be attempted once. If this function blocks it will
	// halt reconnect progress. It will be called from a single goroutine.
	Retry func(chan struct{}, func() error) error
	// contains filtered or unexported fields
}

TextClient is a WebSocket text client that automatically reconnects to the remote service. All fields that you decide to set must be set before calling Open and are not safe to be modified after. When a connection problem occurs, writes will fail and some incoming messages may be lost. The client can only be opened and closed once.

func (*TextClient) Close

func (c *TextClient) Close() error

Close sends a close frame and then closes the underlying connection. It will block until a full shutdown has been achieved.

func (*TextClient) Open

func (c *TextClient) Open(dialer Dialer) error

Open opens the connection to the given URL and starts receiving messages

func (*TextClient) WriteTextMessage

func (c *TextClient) WriteTextMessage(msg []byte) error

WriteTextMessage writes a text message to the WebSocket

Jump to

Keyboard shortcuts

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