websockets

package
v0.0.0-...-185a503 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2017 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package websockets is a middleware that provides WebSockets channels binding for bigo.

Index

Constants

This section is empty.

Variables

View Source
var LogLevelStrings = []string{"Error", "Warning", "Info", "Debug"}

Log Level to strings slice

Functions

func JSON

func JSON(bindStruct interface{}, options ...*Options) bigo.Handler

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

func Messages(options ...*Options) bigo.Handler

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 Binding

type Binding interface {
	Close(int) error

	DisconnectChannel() chan int
	ErrorChannel() chan error
	// contains filtered or unexported methods
}

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 []byte

	// 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 []byte
}

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 *bigo.Logger

	// The LogLevel for socket logging, goes from 0 (Error) to 3 (Debug)
	LogLevel bigo.LogLevel

	// 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
}

Jump to

Keyboard shortcuts

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