jsonws

package
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2020 License: Apache-2.0 Imports: 5 Imported by: 4

Documentation

Overview

Package jsonws defines functionality to deal with web sockets and JSON.

Index

Constants

View Source
const (
	// DefaultWebSocketReadBufferSize is the default size of the web socket
	// read buffer in bytes.
	DefaultWebSocketReadBufferSize = 1024

	// DefaultWebSocketWriteBufferSize is the default size of the web socket
	// write buffer in bytes.
	DefaultWebSocketWriteBufferSize = 1024

	// DefaultWebSocketWriteChanSize is the default size of a web socket
	// buffered connection channel.
	DefaultWebSocketWriteChanSize = 256

	// DefaultWebSocketWriteTimeout is the default timeout of a web socket
	// write.
	DefaultWebSocketWriteTimeout = 10 * time.Second

	// DefaultWebSocketPongTimeout is the default timeout of a web socket
	// expected pong.
	DefaultWebSocketPongTimeout = time.Minute

	// DefaultWebSocketPingInterval is the default interval between web
	// socket pings.
	DefaultWebSocketPingInterval = (DefaultWebSocketPongTimeout * 9) / 10

	// DefaultWebSocketMaxMsgSize is the default maximum size of a web
	// socke received message in in bytes.
	DefaultWebSocketMaxMsgSize = 32 * 1024
)

Variables

This section is empty.

Functions

func DefaultBasicMsgAllocator

func DefaultBasicMsgAllocator(msg *interface{})

DefaultBasicMsgAllocator is the default function that allocates a message type before unmarshalling it. It allocate a map of strings to interface{}.

Types

type Basic

type Basic struct {
	*Hub
	// contains filtered or unexported fields
}

Basic implements basic web socket server meant to be used in conjunction with an HTTP server.

func NewBasic

func NewBasic(config *BasicConfig, bufConnConfig *BufferedConnConfig) *Basic

NewBasic creates a new basic web socket server.

func (*Basic) AddConnChannel

func (s *Basic) AddConnChannel(c chan *BufferedConn)

AddConnChannel adds a channel that will be sent new connections.

func (*Basic) AddMsgChannel

func (s *Basic) AddMsgChannel(c chan BasicConnMsg)

AddMsgChannel adds a channel that will be sent messages received by connections.

func (*Basic) Handle

func (s *Basic) Handle(w http.ResponseWriter, r *http.Request)

Handle handles an HTTP request for a web socket connection. The web socket route of the HTTP server should pass the writer and request to this function.

type BasicConfig

type BasicConfig struct {
	ReadBufferSize  int               // Size of the read buffer in bytes
	WriteBufferSize int               // Size of the write buffer in bytes
	UpgradeHandle   UpgradeHandle     // Optional custom HTTP request upgrader
	MsgAllocator    BasicMsgAllocator // Optional custom message allocator
	EnableCORS      bool              // Optionally enable cross-origin requests
}

BasicConfig contains options for a basic web socket server.

type BasicConnMsg

type BasicConnMsg struct {
	Conn *BufferedConn
	Msg  interface{}
}

BasicConnMsg contains a connection and a message read from that connection.

type BasicMsgAllocator

type BasicMsgAllocator func(*interface{})

BasicMsgAllocator is a function that must initialize a message type before it is unmarshallied. A custom BasicMsgAllocator should be used when reading JSON messages so that they can be unmarshalled to an appropriate type. for instance:

func MyMsgAllocator(msg *interface{}) {
	*msg = MyCustomMessageType{}
}

type BufferedConn

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

BufferedConn wraps a connection so that writes are buffered and not blocking unless the channel is full. Is it a higher level type that also deals with control messages and timeouts. It requires an underlying PingableConn.

func NewBufferedConn

func NewBufferedConn(conn PingableConn, config *BufferedConnConfig) *BufferedConn

NewBufferedConn creates a new buffered connection from a pingable connection.

func (*BufferedConn) Close

func (c *BufferedConn) Close() error

Close closes the connection.

func (*BufferedConn) ReadJSON

func (c *BufferedConn) ReadJSON(v interface{}) error

ReadJSON reads JSON from the connection.It blocks until a value is received

func (*BufferedConn) Start

func (c *BufferedConn) Start() error

Start starts the buffered connection. It will stop with an error if a write failed. It will also ping the connection at regular intervals.

func (*BufferedConn) WriteJSON

func (c *BufferedConn) WriteJSON(v interface{}) error

WriteJSON writes JSON to the connection.

type BufferedConnConfig

type BufferedConnConfig struct {
	Size         int           // Size of the write channel
	WriteTimeout time.Duration // Time allowed to write a message
	PongTimeout  time.Duration // Time allowed to read next pong
	PingInterval time.Duration // Interval between two pings (< PongTimeout)
	MaxMsgSize   int64         // Maximum size of input message in bytes
}

BufferedConnConfig contains options for a buffered connection.

type Conn

type Conn interface {
	Writer

	// Closes the connection.
	Close() error

	// Reads JSON from the connection. It blocks until a value is received.
	ReadJSON(v interface{}) error

	// SetReadLimit sets the maximum size for a message read.
	SetReadLimit(limit int64)

	// SetReadDeadline sets the read deadline.
	SetReadDeadline(t time.Time) error

	// SetWriteDeadline sets the write deadline.
	SetWriteDeadline(t time.Time) error

	// SetPongHandler sets the handler for pong messages received.
	SetPongHandler(h func(appData string) error)
}

Conn must be implemented by a web socket connection.

type GorrilaConn

type GorrilaConn struct {
	*websocket.Conn
}

GorrilaConn implements github.com/stratumn/go-core/jsonws/Conn using a Gorrila web socket connection.

func (GorrilaConn) Ping

func (c GorrilaConn) Ping() error

Ping implements github.com/stratumn/go-core/jsonws/Conn.Ping.

type Hub

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

Hub manages a list of web socket connections. Messages can be broadcasted in JSON form to the list of connections. Connections may also be tagged, and messages can be broadcasted only to connections that have a certain tag.

func NewHub

func NewHub() *Hub

NewHub creates a new hub.

func (*Hub) Broadcast

func (h *Hub) Broadcast(msg interface{}, tag interface{})

Broadcast broadcasts the JSON representation of a message. If tag is nil, it broadcasts the message to every connection. Otherwise it broadcasts the message only to connections that have that tag.

func (*Hub) Register

func (h *Hub) Register(conn Writer)

Register adds a connection to the list.

func (*Hub) Start

func (h *Hub) Start()

Start starts managing the client connections.

func (*Hub) Stop

func (h *Hub) Stop()

Stop stops managing the client connections.

func (*Hub) Tag

func (h *Hub) Tag(conn Writer, tag interface{})

Tag adds a tag to a connection.

func (*Hub) Unregister

func (h *Hub) Unregister(conn Writer)

Unregister removes a connection from the list.

func (*Hub) Untag

func (h *Hub) Untag(conn Writer, tag interface{})

Untag remotes a tag from a connection.

type Message added in v0.2.0

type Message struct {
	Type string      `json:"type"`
	Data interface{} `json:"data"`
}

Message is a web socket message.

type PingableConn

type PingableConn interface {
	Conn

	// Sends a ping control message.
	Ping() error
}

PingableConn must be able to send a ping control message.

type UpgradeHandle

type UpgradeHandle func(w http.ResponseWriter, r *http.Request, h http.Header) (PingableConn, error)

UpgradeHandle is a function that upgrades an HTTP connection to a web socket connection.

type Writer

type Writer interface {
	// Writes JSON to the connection.
	WriteJSON(v interface{}) error
}

Writer must be able to write JSON messages.

Directories

Path Synopsis
Package jsonwstesting defines helpers to test web sockets.
Package jsonwstesting defines helpers to test web sockets.

Jump to

Keyboard shortcuts

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