websocket

package
v0.0.0-...-432a7fd Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2018 License: Zlib Imports: 14 Imported by: 5

Documentation

Index

Constants

View Source
const Extended16BitLength = 126

Constant used on the Length field if the length actually has 16 bits.

View Source
const Extended64BitLength = 127

Constant used on the Length field if the length actually has 64 bits.

View Source
const FinBit = 0x80

Bit used to indicate that this is the last (or only) part of a message.

View Source
const LengthIndex = 1

Index of the opcode within a Frame.

View Source
const LengthMask = 0x7F

Mask to retrieve the opcode from its byte in the Frame.

View Source
const MaskBit = 0x80

Bit used to check whether "Payload data" is masked.

View Source
const MaskIndex = 1

Index of the mask bit within a Frame.

View Source
const MaskLength = 4

Length of the "Masking-key".

View Source
const MinHeaderLength = 2

Minimal length (in bytes) of a message from a WebSocket.

View Source
const OpcodeIndex = 0

Index of the opcode within a Frame.

View Source
const OpcodeMask = 0x0F

Mask to retrieve the opcode from its byte in the Frame.

View Source
const Strict = false

Currently, only ignores if no |Host| was supplied in the header.

View Source
const WS_SERVER_ID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

GUID used by every WebSocket server (as specified on the RFC).

Variables

View Source
var HeartBeatMessage = []byte{0x80 | byte(Pong), 0x00}

Default message to be used for heart-beats.

Functions

func WrapFrame

func WrapFrame(opcode Opcode, data, buf []byte) (retBuf []byte)

WrapFrame data. If buf is supplied, the WebSocket frame is written into it, avoiding allocing memory as much as possible.

Types

type ClientConnection

type ClientConnection interface {
	// Send buf through the web-socket, wrapping it into a WebSocket frame and
	// as text data. Use 'Send' if you need to specify the type.
	Write(buf []byte) (int, error)
	// Send buf through the web-socket, wrapping it into a WebSocket frame and
	// as the specified type.
	Send(buf []byte, dataType Opcode) error
	// Send data throught the WebSocket as-is. Shouldn't be used unless you know
	// what you are doing!
	SendRaw(buf []byte) error
	// Requests to close the connection with the client.
	Close(code CloseReason, reason []byte) error
	// Release cached memory. Should be used for long-living connections that
	// occasionally sends really big messages.
	Flush()
}

Ugly (and probably non-convetional) interface that exports the only public functions of connections to clients. None of these are thread-safe. However, since they should only be called from the goroutine handling the client's connection, this shouldn't be an issue...

type CloseReason

type CloseReason uint16
const (
	NormalClosure       CloseReason = 1000
	GoingAway           CloseReason = 1001
	ProtocolError       CloseReason = 1002
	InvalidDataType     CloseReason = 1003
	InconsistentContent CloseReason = 1007
	InvalidMessage      CloseReason = 1008
	MessageTooBig       CloseReason = 1009
	MissingExtension    CloseReason = 1010
	UnexpectedError     CloseReason = 1011
)

type Context

type Context struct {
	// Local IP where connections are accept (blank for accepting globally).
	Ip string
	// URI for accepting connections (e.g., 'ws://Ip:Port/Uri').
	Uri string
	// Port where the WebSocket will listen to. Cannot be changed after starting
	// the server.
	Port int
	// How many simultaneous connections this server accepts. Anything less or
	// equal to 0 means unlimited.
	MaximumConnections int
	// contains filtered or unexported fields
}

func NewContext

func NewContext(ip, uri string, port, maxConn int) *Context

Creates a new context. The context may be manually instantiated as well (as long as all public fields are set).

func (*Context) Close

func (ctx *Context) Close()

Closes and stops the listening server.

func (*Context) Run

func (ctx *Context) Run(cerr chan error)

Runs the websocket server and blocks execution until 'Stop' is called. Can (and should) safelly be called from/as a goroutine. If it fails to accept any connection, the error is reported through the supplied channel (if any).

The server signals that it has exited by sending a nil error to the channel.

func (*Context) SetTlsConfig

func (ctx *Context) SetTlsConfig(config *tls.Config)

Set the new TLS configuration. Open connections will still use the old credentials, but new connections will automatically use this new one.

func (*Context) Setup

func (ctx *Context) Setup(serverTempl Server) error

Set everything up so the conectext may accept connections.

type Opcode

type Opcode uint8

Defines the interpretation of the "Payload data". Only the lowest 4 bits are used.

const (
	ContinuationFrame Opcode = 0x0
	TextFrame         Opcode = 0x1
	BinaryFrame       Opcode = 0x2
	// 0x3...0x7: reserved for further non-control frames
	ConnectionClose Opcode = 0x8
	Ping            Opcode = 0x9
	Pong            Opcode = 0xA
)

type Server

type Server interface {
	// Returns a new instance of this server, passed to the runner.
	Clone(conn ClientConnection) (Server, error)
	// Do something with the received message. Offset points to the actual
	// payload within the WebSocket frame.
	Do(msg []byte, offset int) error
	// Called after the connection to this client was closed.
	Cleanup()
}

Jump to

Keyboard shortcuts

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