websocket

package module
v0.0.0-...-cfd4ec1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2016 License: MIT Imports: 16 Imported by: 0

README

Go Report Card GoDoc

websocket

Package websocket implements the websocket protocol defined in rfc6455

Installation

go get github.com/tabone/websocket

Documentation

Documentation

Overview

Package websocket implements the websocket protocol defined in rfc6455.

Index

Constants

View Source
const (
	OpcodeContinuation int = 0
	OpcodeText         int = 1
	OpcodeBinary       int = 2
	OpcodeClose        int = 8
	OpcodePing         int = 9
	OpcodePong         int = 10
)

WebSocket Opcodes. Ref Spec: https://tools.ietf.org/html/rfc6455#section-5.2

View Source
const (
	CloseNormalClosure           int = 1000
	CloseGoingAway               int = 1001
	CloseProtocolError           int = 1002
	CloseUnsupportedData         int = 1003
	CloseNoStatusReceived        int = 1005
	CloseAbnormalClosure         int = 1006
	CloseInvalidFramePayloadData int = 1007
	ClosePolicyViolation         int = 1008
	CloseMessageTooBig           int = 1009
	CloseMandatoryExtension      int = 1010
	CloseInternalServerErr       int = 1011
	CloseTLSHandshake            int = 1015
)

WebSocket Error codes. Ref Spec: https://tools.ietf.org/html/rfc6455#section-7.4.1

Variables

View Source
var ErrSocketClosed = errors.New("socket has been closed")

ErrSocketClosed is the error returned when a user tries to send a frame with a closed socket.

Functions

This section is empty.

Types

type CloseError

type CloseError struct {
	Code   int
	Reason string
}

CloseError represents errors related to the websocket closing handshake.

func NewCloseError

func NewCloseError(b []byte) (*CloseError, error)

NewCloseError is used to create a new CloseError instance by parsing 'b'. In order for this to happen the []bytes needs to conform with the way the websocket rfc expects the payload data of CLOSE FRAMES to be.

While parsing if the error code (i.e. first two bytes) is invalid, it will default the CloseError instance returned to represent a 'No Status Received Error' (i.e. 1005).

Ref Spec: https://tools.ietf.org/html/rfc6455#section-5.5.1

func (*CloseError) Error

func (c *CloseError) Error() string

Error implements the built in error interface.

func (*CloseError) ToBytes

func (c *CloseError) ToBytes() ([]byte, error)

ToBytes returns the representation of a CloseError instance in a []bytes that conforms with the way the websocket rfc expects the payload data of CLOSE FRAMES to be.

While generating the []bytes, if the CloseError instance has an invalid error code, it will instead create the representation of a 'No Status Received Error' (i.e. 1005).

Ref Spec: https://tools.ietf.org/html/rfc6455#section-5.5.1

type Dialer

type Dialer struct {
	/*
		Header to be included in the opening handshake request.
	*/
	Header http.Header

	/*
		SubProtocols which the client supports.
	*/
	SubProtocols []string

	/*
		TLSConfig is used to configure the TLS client.
	*/
	TLSConfig *tls.Config
}

Dialer is a websocket client.

func (*Dialer) Dial

func (d *Dialer) Dial(u string) (*Socket, *http.Response, error)

Dial is the method used to start the websocket connection.

type OpenError

type OpenError struct {
	Reason string
}

OpenError represents errors related to the websocket opening handshake.

func (*OpenError) Error

func (h *OpenError) Error() string

Error implements the built in error interface.

type Request

type Request struct {

	/*
		CheckOrigin is the function which will be used to validate the ORIGIN
		HTTP Header of the request. By default this method will fail the opening
		handshake when the origin is not the same. This method can be overridden
		during the initiation of the Request struct.
	*/
	CheckOrigin func(r *http.Request) bool

	/*
		SubProtocol name which the server has agreed to use from the list
		provided by the client (through the Sec-WebSocket-Protocol HTTP Header
		Field). Before sending the servers opening handshake response, checks
		are made to verify that the chosen protocol was indeed been provided as
		an option from the client. If this is not the case, the HTTP
		Sec-WebSocket-Protocol HTTP Response Header Field is not sent
	*/
	SubProtocol string
	// contains filtered or unexported fields
}

Request represents the HTTP Request that will be upgraded to the WebSocket protocol once it is validated.

func (*Request) ClientExtensions

func (q *Request) ClientExtensions() []string

ClientExtensions returns the list of Extensions the client can interact with.

From spec: https://tools.ietf.org/html/rfc6455#section-4.2.1

func (*Request) ClientSubProtocols

func (q *Request) ClientSubProtocols() []string

ClientSubProtocols returns the list of Sub Protocols the client can interact with.

From spec: https://tools.ietf.org/html/rfc6455#section-4.2.1

func (*Request) Upgrade

func (q *Request) Upgrade(w http.ResponseWriter, r *http.Request) (*Socket, error)

Upgrade is used to upgrade the HTTP connection to use the WS protocol once the client request is validated.

type Socket

type Socket struct {

	/*
		closeDelay is the duration the socket instance will wait until it closes
		the underlying tcp connection once the closing handshake has been
		completed.

		The websocket rfc suggests that when the closing handshake is completed
		the underlying tcp connection should first be terminated by the server
		endpoint. Having said this it doesn't restrict the client endpoint to do
		so itself. CloseDelay is the maximum time the socket instance will wait
		before it closes the tcp connection.

		Note: Server endpoints should always have this property set to 0.

		Ref Spec: https://tools.ietf.org/html/rfc6455#section-5.5.1
	*/
	CloseDelay time.Duration

	/*
		readHandler is invoked whenever a text or binary frame is received. The
		opcode and payload data are provided as args respectively.
	*/
	ReadHandler func(int, []byte)

	/*
		pingHandler is invoked whenever a ping frame is received. The payload
		data is provided as arg.
	*/
	PingHandler func([]byte)

	/*
		pongHandler is invoked whenever a pong frame is received. The payload
		data is provided as arg.
	*/
	PongHandler func([]byte)

	/*
		closeHandler is invoked whenever the websocket connection is closed. The
		reason for the closure is provided as an arg.
	*/
	CloseHandler func(error)
	// contains filtered or unexported fields
}

Socket represents a socket endpoint.

func (*Socket) Close

func (s *Socket) Close()

Close initiates the normal closures (1000) closing handshake.

func (*Socket) CloseWithError

func (s *Socket) CloseWithError(e *CloseError)

CloseWithError initiates the closing handshake.

func (*Socket) Listen

func (s *Socket) Listen()

Listen is used to start listening for new frames sent by the connected endpoint.

func (*Socket) SetReadDeadline

func (s *Socket) SetReadDeadline(t time.Time)

SetReadDeadline sets the deadline for future Read calls. A zero value for t means Read will not time out.

func (*Socket) SetWriteDeadline

func (s *Socket) SetWriteDeadline(t time.Time)

SetWriteDeadline sets the deadline for future Write calls. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means Write will not time out.

func (*Socket) TCPClose

func (s *Socket) TCPClose()

TCPClose closes the underlying tcp connection if it hasn't already been closed.

func (*Socket) WriteMessage

func (s *Socket) WriteMessage(o int, p []byte) error

WriteMessage is used to send frames to the connected endpoint. It accepts two arguments 'o' opcode, 'p' payload data.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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