socketcrutch

package module
v0.0.0-...-1b013b7 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2016 License: MIT Imports: 13 Imported by: 1

README

SocketCrutch

A extendable Go WebSocket-framework

Quick start

Installation

go get github.com/ZloyDyadka/socketcrutch
Simple example


import (
	"github.com/ZloyDyadka/socketcrutch"
	"github.com/ZloyDyadka/socketcrutch/codec"
	"log"
	"net/http"
)

func main() {
	server := socketcrutch.New()
	server.SetCodec(&codec.MsgPackCodec{})
	
	router := server.GetRouter()
	
	api := router.Group("v1")
	
	api.Route("test", func(session *socketcrutch.Session, data []byte) error {
	    log.Println("Received a test message")
	    
	    return nil
	})
	
	
	http.HandleFunc("/ws", server.ServeHTTP)

	err := http.ListenAndServe(":8080", nil)

	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

TODO


  • Testing
  • Ack message
  • Client library
  • Documentation

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

View Source
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

View Source
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 ErrorHandler func(*Session, error)

type HandlerFunc

type HandlerFunc func(*Session, []byte) error

type HandshakeHandler

type HandshakeHandler func(http.ResponseWriter, *http.Request) bool

type MiddlewareFunc

type MiddlewareFunc func(*Session, []byte)

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 Route

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

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 RouteStore

type RouteStore interface {
	Get(string) *Route
	Set(string, *Route)
}

type Router

type Router struct {
	RouteGroup
	// contains filtered or unexported fields
}

func (*Router) SetRouteSeparator

func (r *Router) SetRouteSeparator(separator string)

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) Close

func (s *Session) Close() error

Close closes the connection.

func (*Session) GetDetail

func (s *Session) GetDetail(key string) (interface{}, bool)

GetDetail returns a value of the details. GetDetail is thread-safe

func (*Session) ID

func (s *Session) ID() string

Request returns the session ID.

func (*Session) Publish

func (s *Session) Publish(event string, payload []byte) error

Publish publishes the event with provided data. The data will be automatically packed.

func (*Session) RemoteAddr

func (s *Session) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*Session) Request

func (s *Session) Request() *http.Request

Request returns the first HTTP request when established connection.

func (*Session) SetDetail

func (s *Session) SetDetail(key string, value interface{})

SetDetail sets a value of the details. SetDetail is thread-safe.

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

func (c *SocketCrutch) SetPingInterval(t time.Duration) *config

SetPingInterval sets ping interval. By default ping interval is 30 seconds.

func (*SocketCrutch) SetPingTimeout

func (c *SocketCrutch) SetPingTimeout(t time.Duration) *config

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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