Documentation ¶
Overview ¶
Package jsonws defines functionality to deal with web sockets and JSON.
Index ¶
Constants ¶
This section is empty.
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 conjuction 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.
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 }
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) 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 regulart 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 ¶
GorrilaConn implements github.com/stratumn/sdk/jsonws/Conn using a Gorrila web socket connection.
func (GorrilaConn) Ping ¶
func (c GorrilaConn) Ping() error
Ping implements github.com/stratumn/sdk/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 (*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) Unregister ¶
Unregister removes a connection from the list.
type PingableConn ¶
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.
Directories ¶
Path | Synopsis |
---|---|
Package jsonwstesting defines helpers to test web sockets.
|
Package jsonwstesting defines helpers to test web sockets. |