Documentation ¶
Overview ¶
The sockets package implements a middleware for standard websocket handling in Martini using the RFC 6455 compliant gorilla implementation of the websocket protocol (github.com/gorilla/websocket). It maps the websocket connection to channels and takes care of connection setup and destruction in order to facilitate easier setup & use of websocket connections.
Index ¶
Constants ¶
const ( // Log levels 0-4. Use to set the log level you wish to go for LogLevelError = 0 LogLevelWarning = 1 LogLevelInfo = 2 LogLevelDebug = 3 )
Variables ¶
var LogLevelStrings = []string{"Error", "Warning", "Info", "Debug"}
Log Level to strings slice
Functions ¶
func JSON ¶
JSON returns a websocket handling middleware. It can only be used in handlers for HTTP GET. IMPORTANT: The last handler in your handler chain must block in order for the connection to be kept alive. It accepts an empty struct it will copy and try to populate with data received from the client using the JSON Marshaler, as well as it will serialize your structs to JSON and send them to the client. For the following, it is assumed you passed a struct named Message to the handler. It maps four channels for you to use in the follow-up Handler(s):
- A receiving string channel (<-chan *Message) on which you will receive all incoming structs from the client
- A sending string channel (chan<- *Message) on which you will be able to send structs to the client.
- A receiving error channel (<-chan error) on which you will receive errors occurring while sending & receiving
- A receiving disconnect channel (<-chan bool) on which you will receive a message only if the connection is about to be closed following an error or a client disconnect.
- A sending done channel (chan<- bool) on which you can send as soon as you wish to disconnect the connection.
The middleware handles the following for you: - Checking the request for cross origin access - Doing the websocket handshake - Setting sensible options for the Gorilla websocket connection - Starting and terminating the necessary goroutines An optional sockets.Options object can be passed to Messages to overwrite default options mentioned in the documentation of the Options object.
func Messages ¶
Messages returns a websocket handling middleware. It can only be used in handlers for HTTP GET. IMPORTANT: The last handler in your handler chain must block in order for the connection to be kept alive. It maps four channels for you to use in the follow-up Handler(s):
- A receiving string channel (<-chan string) on which you will receive all incoming strings from the client
- A sending string channel (chan<- string) on which you will be able to send strings to the client.
- A receiving error channel (<-chan error) on which you will receive errors occurring while sending & receiving
- A receiving disconnect channel (<-chan bool) on which you will receive a message only if the connection is about to be closed following an error or a client disconnect.
- A sending done channel (chan<- bool) on which you can send as soon as you wish to disconnect the connection.
The middleware handles the following for you: - Checking the request for cross origin access - Doing the websocket handshake - Setting sensible options for the Gorilla websocket connection - Starting and terminating the necessary goroutines An optional sockets.Options object can be passed to Messages to overwrite default options mentioned in the documentation of the Options object.
Types ¶
type Connection ¶
type Connection struct { *Options // The error channel is given the error object as soon as an error occurs // either sending or receiving values from the websocket. This channel gets // mapped for the next handler to use. Error chan error // The disconnect channel is for listening for disconnects from the next handler. // Any sends to the disconnect channel lead to disconnecting the socket with the // given closing message. This channel gets mapped for the next // handler to use. Disconnect chan int // The done channel gets called only when the connection // has been successfully disconnected. Any sends to the disconnect // channel are currently ignored. This channel gets mapped for the next // handler to use. Done chan bool // contains filtered or unexported fields }
func (*Connection) Close ¶
func (c *Connection) Close(closeCode int) error
Close the Base connection. Closes the send Handler and all channels used Since all channels are either internal or channels this middleware is sending on.
func (*Connection) DisconnectChannel ¶
func (c *Connection) DisconnectChannel() chan int
func (*Connection) ErrorChannel ¶
func (c *Connection) ErrorChannel() chan error
type JSONConnection ¶
type JSONConnection struct { *Connection // Sender is the channel used for sending out JSON to the client. // This channel gets mapped for the next handler to use with the right type // and is asynchronous unless the SendChannelBuffer is set to 0. Sender reflect.Value // Receiver is the string channel used for receiving JSON from the client. // This channel gets mapped for the next handler to use with the right type // and is asynchronous unless the RecvChannelBuffer is set to 0. Receiver reflect.Value }
Message Connection connects a websocket message connection to a reflect.Value channel.
func (*JSONConnection) Close ¶
func (c *JSONConnection) Close(closeCode int) error
Close the JSON connection. Closes the send goroutine and all channels used Except for the send channel, since it should be closed by the handler sending on it.
type MessageConnection ¶
type MessageConnection struct { *Connection // Sender is the string channel used for sending out strings to the client. // This channel gets mapped for the next handler to use and is asynchronous // unless the SendChannelBuffer is set to 0. Sender chan string // Receiver is the string channel used for receiving strings from the client. // This channel gets mapped for the next handler to use and is asynchronous // unless the RecvChannelBuffer is set to 0. Receiver chan string }
Message Connection connects a websocket message connection to a string channel.
func (*MessageConnection) Close ¶
func (c *MessageConnection) Close(closeCode int) error
Close the Message connection. Closes the send goroutine and all channels used Except for the send channel, since it should be closed by the handler sending on it.
type Options ¶
type Options struct { // The logger to use for socket logging Logger *log.Logger // The LogLevel for socket logging, goes from 0 (Error) to 3 (Debug) LogLevel int // Set to true if you want to skip logging SkipLogging bool // The time to wait between writes before timing out the connection // When this is a zero value time instance, write will never time out WriteWait time.Duration // The time to wait at maximum between receiving pings from the client. PongWait time.Duration // The time to wait between sending pings to the client PingPeriod time.Duration // The maximum messages size for receiving and sending in bytes MaxMessageSize int64 // The send channel buffer SendChannelBuffer int // The receiving channel buffer RecvChannelBuffer int }