Documentation ¶
Overview ¶
Copyright 2016 Ilya Galimyanov All rights reserved. Use of this source code is governed by a MIT style license that can be found in the LICENSE file.SE OR OTHER DEALINGS IN THE SOFTWARE.
Index ¶
- Constants
- Variables
- type Codec
- type Conn
- type ErrorHandler
- type HandlerFunc
- type HandshakeHandler
- type MiddlewareFunc
- type Packet
- type PacketType
- type Route
- type RouteGroup
- type RouteStore
- type Router
- type Session
- func (s *Session) Close() error
- func (s *Session) GetDetail(key string) (interface{}, bool)
- func (s *Session) ID() string
- func (s *Session) Publish(event string, payload []byte) error
- func (s *Session) RemoteAddr() net.Addr
- func (s *Session) Request() *http.Request
- func (s *Session) SetDetail(key string, value interface{})
- type SessionHandler
- type SocketCrutch
- func (sc *SocketCrutch) GetRouter() *Router
- func (sc *SocketCrutch) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (sc *SocketCrutch) SetCodec(p Codec) *SocketCrutch
- func (sc *SocketCrutch) SetConnectHandler(fn SessionHandler) *SocketCrutch
- func (sc *SocketCrutch) SetDisconnectHandler(fn SessionHandler) *SocketCrutch
- func (sc *SocketCrutch) SetErrorHandler(fn ErrorHandler) *SocketCrutch
- func (sc *SocketCrutch) SetHandshakeHandler(fn HandshakeHandler) *SocketCrutch
- func (c *SocketCrutch) SetMaxConnections(max int32) *config
- func (c *SocketCrutch) SetMessageBufferSize(size int) *config
- func (sc *SocketCrutch) SetOriginHandler(f func(*http.Request) bool) *SocketCrutch
- func (c *SocketCrutch) SetPingInterval(t time.Duration) *config
- func (c *SocketCrutch) SetPingTimeout(t time.Duration) *config
- func (c *SocketCrutch) SetReadBufferSize(size int) *config
- func (c *SocketCrutch) SetReadLimit(limit int64) *config
- func (c *SocketCrutch) SetWriteBufferSize(size int) *config
Constants ¶
const ( // BinaryMessage denotes a binary data message. BinaryMessage = websocket.BinaryMessage // TextMessage denotes a text data message. The text message payload is // interpreted as UTF-8 encoded text data. TextMessage = websocket.TextMessage // The ID for not ack packet WithoutID = -1 )
Variables ¶
var (
ErrSend = errors.New("the message buffer full or the connection is closed")
)
Functions ¶
This section is empty.
Types ¶
type Codec ¶
type Codec interface { // NewPacket returns the empty new packet. NewPacket() Packet // Returns read mode, that should be used for this codec. ReadMode() int // Returns write mode, that should be used for this codec WriteMode() int }
Codec defines the interface that a codec should implement. A codec should be able to marshal/unmarshal messages from the given bytes.
type Conn ¶
type Conn interface { // Send sends the given raw data to the websocket connection. Send([]byte) error // Close closes the underlying websocket connection. Close() error // NextMessage returns the next data message received from the connection. NextMessage() ([]byte, bool) // RemoteAddr returns the remote network address. RemoteAddr() net.Addr // Codec returns the established codec. Codec() Codec }
type ErrorHandler ¶
type HandlerFunc ¶
type HandshakeHandler ¶
type HandshakeHandler func(http.ResponseWriter, *http.Request) bool
type MiddlewareFunc ¶
type Packet ¶
type Packet interface { // EventName returns the event name from packet. EventName() string // SetEventName sets the name for this packet. SetEventName(string) // Type returns the type from the packet. Type() PacketType // SetType sets the name for this packet. SetType(PacketType) // Payload returns the payload from the packet. Payload() []byte // SetPayload sets the payload for this packet. SetPayload([]byte) // ID returns the id from the packet. ID() int // SetID sets the id for this packet. SetID(int) //Unmarshal parses the encoded data structure and stores the result in the Packet. Unmarshal([]byte) error //Marshal returns the data structure encoding of Packet. Marshal() ([]byte, error) }
Packet is a container for the raw data and therefore it holds the event name, pointer to data and id of the event type.
type PacketType ¶
type PacketType byte
const ( EventType PacketType = iota AckType )
func (PacketType) String ¶
func (t PacketType) String() string
type RouteGroup ¶
type RouteGroup struct {
// contains filtered or unexported fields
}
RouteGroup represents a group of routes that share the same path prefix.
func (*RouteGroup) Group ¶
func (g *RouteGroup) Group(prefix string, handlers ...HandlerFunc) *RouteGroup
Group creates a RouteGroup with the given route path prefix and handlers. The new group will combine the existing path prefix with the new one. If no handler is provided, the new group will inherit the handlers registered with the current group.
func (*RouteGroup) Route ¶
func (g *RouteGroup) Route(path string, handlers ...HandlerFunc)
Route adds a route to the router with the given route path and handlers.
func (*RouteGroup) Use ¶
func (g *RouteGroup) Use(handlers ...HandlerFunc)
Use registers one or multiple handlers to the current route group. These handlers will be shared by all routes belong to this group and its subgroups.
type Router ¶
type Router struct { RouteGroup // contains filtered or unexported fields }
func (*Router) SetRouteSeparator ¶
SetRouteSeparator sets the character as a separator between the route parts.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents an active SocketCrutch session.
func (*Session) Publish ¶
Publish publishes the event with provided data. The data will be automatically packed.
func (*Session) RemoteAddr ¶
RemoteAddr returns the remote network address.
type SessionHandler ¶
type SessionHandler func(*Session)
type SocketCrutch ¶
type SocketCrutch struct {
// contains filtered or unexported fields
}
SocketCrutch is server of the websocket
func New ¶
func New() *SocketCrutch
New returns a SocketCrutch instance with default Upgrader, Config, Handlers.
func (*SocketCrutch) GetRouter ¶
func (sc *SocketCrutch) GetRouter() *Router
func (*SocketCrutch) ServeHTTP ¶
func (sc *SocketCrutch) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
func (*SocketCrutch) SetCodec ¶
func (sc *SocketCrutch) SetCodec(p Codec) *SocketCrutch
SetCodec sets the codec of the connection to the supplied implementation of the Codec interface.
func (*SocketCrutch) SetConnectHandler ¶
func (sc *SocketCrutch) SetConnectHandler(fn SessionHandler) *SocketCrutch
SetConnectHandler sets the callback, that is called when a websocket connection was successfully established.
func (*SocketCrutch) SetDisconnectHandler ¶
func (sc *SocketCrutch) SetDisconnectHandler(fn SessionHandler) *SocketCrutch
SetDisconnectHandler sets the callback, that is called when the connection is closed.
func (*SocketCrutch) SetErrorHandler ¶
func (sc *SocketCrutch) SetErrorHandler(fn ErrorHandler) *SocketCrutch
SetErrorHandler sets the callback, that is called when in connection error has occurred.
func (*SocketCrutch) SetHandshakeHandler ¶
func (sc *SocketCrutch) SetHandshakeHandler(fn HandshakeHandler) *SocketCrutch
SetHandshakeHandler sets the callback for handshake verification.
func (*SocketCrutch) SetMaxConnections ¶
func (c *SocketCrutch) SetMaxConnections(max int32) *config
SetMaxConnections sets maximum numbers of connections. By default amount connections is 1024.
func (*SocketCrutch) SetMessageBufferSize ¶
func (c *SocketCrutch) SetMessageBufferSize(size int) *config
SetMessageBufferSize sets the maximum amount of messages in the buffer. The max amount of messages that can be in a sessions buffer before it starts dropping them. By default buffer size is 256.
func (*SocketCrutch) SetOriginHandler ¶
func (sc *SocketCrutch) SetOriginHandler(f func(*http.Request) bool) *SocketCrutch
SetCheckOrigin sets the callback for the request Origin header validation.
func (*SocketCrutch) SetPingInterval ¶
SetPingInterval sets ping interval. By default ping interval is 30 seconds.
func (*SocketCrutch) SetPingTimeout ¶
SetPingTimeout sets ping timeout. By default ping timeout is 30 seconds.
func (*SocketCrutch) SetReadBufferSize ¶
func (c *SocketCrutch) SetReadBufferSize(size int) *config
SetReadBufferSize set specify I/O buffer sizes. The I/O buffer sizes do not limit the size of the messages that can be received. By default read buffer size is 4096
func (*SocketCrutch) SetReadLimit ¶
func (c *SocketCrutch) SetReadLimit(limit int64) *config
SetReadLimit sets the maximum size for a message read from the peer. By default read limit size is 1024.
func (*SocketCrutch) SetWriteBufferSize ¶
func (c *SocketCrutch) SetWriteBufferSize(size int) *config
SetWriteBufferSize set specify I/O buffer sizes. The I/O buffer sizes do not limit the size of the messages that can be sent. By default write buffer size is 4096