websocket

package
v0.0.0-...-2842cdc Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2018 License: ISC Imports: 7 Imported by: 0

Documentation

Overview

Package websocket provides a wrapper for github.com/gorilla/websocket. The wrapper has limited features; the point is ease of use for some common cases. It does NOT check the request Origin header. All of its methods are safe for concurrent use. It automatically applies a send timeout. It transparently handles the closing handshake.

Index

Constants

View Source
const (
	// SendTimeout is the amount of time to wait on a Send before giving up.
	SendTimeout = 5 * time.Second

	// CloseRecvTimeout is the amount of time waiting during Close
	// for the remote peer to send a Close message
	// before shutting down the connection.
	CloseRecvTimeout = 5 * time.Second

	// HandshakeTimeout is the amount of time to wait
	// for the connection handshake to complete.
	HandshakeTimeout = 5 * time.Second
)

Variables

View Source
var ErrCloseSent = websocket.ErrCloseSent

ErrCloseSent is returned by Send if sending to a connection that is closing.

Functions

This section is empty.

Types

type Conn

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

A Conn is a websocket connection.

func Dial

func Dial(URL *url.URL) (*Conn, error)

Dial dials a websocket and returns a new Conn.

If the handshake fails, a HandshakeError is returned.

func Upgrade

func Upgrade(w http.ResponseWriter, req *http.Request) (*Conn, error)

Upgrade upgrades an HTTP handler and returns an new *Conn.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the websocket connection, unblocking any blocked calls to Recv or Send, and blocks until the closing handshake completes or CloseRecvTimeout timeout expires.

Close should not be called more than once.

func (*Conn) Recv

func (c *Conn) Recv(msg interface{}) error

Recv receives the next JSON-encoded message into msg. If msg is nill, the received message is discarded.

This function must be called continually until Close() is called, otherwise the connection will not respond to ping/pong messages.

Calling Recv on a closed connection returns io.EOF.

func (*Conn) Send

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

Send sends a JSON-encoded message.

Send must not be called on a closed connection.

type HandshakeError

type HandshakeError struct {
	// Status is the string representation of the HTTP response status code.
	Status string
	// StatusCode is the numeric HTTP response status code.
	StatusCode int
}

A HandshakeError is returned if Dial fails the handshake.

func (HandshakeError) Error

func (err HandshakeError) Error() string

Jump to

Keyboard shortcuts

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