gows

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2021 License: BSD-3-Clause Imports: 6 Imported by: 0

README

hsson/gows (Go Websocket)

PkgGoDev GoReportCard

Websocket servers in Go (Golang) made simple!

gows is a high-level abstraction of a Websocket server that hides away all the boring an complicated details of dealing with a websocket connection. Under the hood, the gorilla/websocket package is used. When using gows, Ping/Pong is automatically handled and you can read and write messages with ease.

Example

The following is an example of a websocket server that simply echoes whatever is being sent from the client:

func echo(w http.ResponseWriter, r *http.Request) {
	ws, err := gows.Upgrade(w, r)
	if err != nil {
		// error handling...
	}

	for {
		msg := <-ws.Read()
		ws.WriteText(msg.Data)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

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

ErrConnectionClosed is returned if trying to perform an operation on a closed connection

View Source
var ErrHandshake = errors.New("websocket handshake error")

ErrHandshake is returned if a request can not be upgraded to a websocket connection

Functions

This section is empty.

Types

type Message

type Message struct {
	Type MessageType
	Data []byte
}

Message has the actual data and the corresponding type that the data was sent as

type MessageType

type MessageType int

MessageType represents a type of websocket message

const (
	TextMessage MessageType = iota + 1
	BinaryMessage
)

MessageType constants

type Option

type Option func(ws *websocket)

Option is used to alter the default behavior of the websocket connection. Options can be optionally passed to the Upgrade function.

func DisablePingPong

func DisablePingPong() Option

DisablePingPong disables the pong/pong mecahnism and means that even if there is no communication the connection will be considered alive.

Default: false

func MaxMessageSize

func MaxMessageSize(size int64) Option

MaxMessageSize defines the maximum size allowed for messages that are read from clients. If a client attempts to send a larger message, an error will occur.

Default: 2048

func PongTimeout

func PongTimeout(t time.Duration) Option

PongTimeout defines how long to wait for a pong response before determining the connection to be dead. Ping messages will be sent in intervals from which pong responses are expected.

Default: 60 seconds

func WriteTimeout

func WriteTimeout(t time.Duration) Option

WriteTimeout defines how long to wait for messages to be sent to the client before aborting. Setting it to zero means no timeout.

Default: 10 seconds

type Websocket

type Websocket interface {
	// WriteText writes data as type Text over the websocket connection
	WriteText(data []byte) error
	// WriteBinary writes data as type Binary over the websocket connection
	WriteBinary(data []byte) error
	// Read subscribes to messages that are sent from the client. If there are
	// more than a single reader, the message will be passed to ONE of the reader
	// at random.
	Read() <-chan Message
	// Close will close the websocket connection
	Close()
	// OnClose can be used to get a signal when the connection is closed. If
	// the connection was closed due to a failure, the error that caused the
	// closing will be returned
	OnClose() chan error
}

Websocket represents a websocket connection on which operations can be performed.

func Upgrade

func Upgrade(w http.ResponseWriter, r *http.Request, opts ...Option) (Websocket, error)

Upgrade attempts to upgrade a HTTP request to a websocket connection. If the upgrade fails, an ErrHandshake error will be returned. Optional options can be passed to alter some details of the connection. See the available options to get their default values.

Jump to

Keyboard shortcuts

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