glue: github.com/desertbit/glue Index | Files | Directories

package glue

import "github.com/desertbit/glue"

Package glue - Robust Go and Javascript Socket Library. This library is thread-safe.

Index

Package Files

channel.go handler.go options.go server.go socket.go

Constants

const (
    // Version holds the Glue Socket Protocol Version as string.
    // This project follows the Semantic Versioning (http://semver.org/).
    Version = "1.9.1"
)

Public ######

Variables

var (
    ErrSocketClosed = errors.New("the socket connection is closed")
    ErrReadTimeout  = errors.New("the read timeout was reached")
)

Public errors:

type Channel Uses

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

A Channel is a separate communication channel.

func (*Channel) DiscardRead Uses

func (c *Channel) DiscardRead()

DiscardRead ignores and discars the data received from this channel. Call this method during initialization, if you don't read any data from this channel. If received data is not discarded, then the read buffer will block as soon as it is full, which will also block the keep-alive mechanism of the socket. The result would be a closed socket...

func (*Channel) OnRead Uses

func (c *Channel) OnRead(f OnReadFunc)

OnRead sets the function which is triggered if new data is received on the channel. If this event function based method of reading data from the socket is used, then don't use the socket Read method. Either use the OnRead or the Read approach.

func (*Channel) Read Uses

func (c *Channel) Read(timeout ...time.Duration) (string, error)

Read the next message from the channel. This method is blocking. One variadic argument sets a timeout duration. If no timeout is specified, this method will block forever. ErrSocketClosed is returned, if the socket connection is closed. ErrReadTimeout is returned, if the timeout is reached.

func (*Channel) Socket Uses

func (c *Channel) Socket() *Socket

Socket returns the channel's socket.

func (*Channel) Write Uses

func (c *Channel) Write(data string)

Write data to the channel.

type ClosedChan Uses

type ClosedChan <-chan struct{}

ClosedChan is a channel which doesn't block as soon as the socket is closed.

type HTTPSocketType Uses

type HTTPSocketType int

A HTTPSocketType defines which socket type to use for the HTTP glue server.

const (
    // HTTPSocketTypeNone defines to not configure and run a HTTP server.
    HTTPSocketTypeNone HTTPSocketType = 1 << iota

    // HTTPSocketTypeTCP defines to use a TCP server.
    HTTPSocketTypeTCP HTTPSocketType = 1 << iota

    // HTTPSocketTypeUnix defines to use a Unix socket server.
    HTTPSocketTypeUnix HTTPSocketType = 1 << iota
)

type OnCloseFunc Uses

type OnCloseFunc func()

OnCloseFunc is an event function.

type OnNewSocketFunc Uses

type OnNewSocketFunc func(s *Socket)

OnNewSocketFunc is an event function.

type OnReadFunc Uses

type OnReadFunc func(data string)

OnReadFunc is an event function.

type Options Uses

type Options struct {
    // HTTPSocketType defines which socket type to use for the HTTP glue server.
    // Default: HTTPSocketTypeTCP
    HTTPSocketType HTTPSocketType

    // The HTTP address to listen on.
    // Default: ":80"
    HTTPListenAddress string

    // HTTPHandleURL defines the base url to handle glue HTTP socket requests.
    // This has to be set, even if the none socket type is used.
    // Default: "/glue/"
    HTTPHandleURL string

    // CheckOrigin returns true if the request Origin header is acceptable. If
    // CheckOrigin is nil, the host in the Origin header must not be set or
    // must match the host of the request.
    // This method is used by the backend sockets before establishing connections.
    CheckOrigin func(r *http.Request) bool

    // Enables the Cross-Origin Resource Sharing (CORS) mechanism.
    // This will set the Access-Control-Allow-Origin HTTP headers.
    // A resource makes a cross-origin HTTP request when it requests a resource
    // from a different domain than the one which served itself.
    EnableCORS bool
}

Options holds the glue server options.

func (*Options) SetDefaults Uses

func (o *Options) SetDefaults()

SetDefaults sets unset option values to its default value.

type Server Uses

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

A Server represents a glue server which handles incoming socket connections.

func NewServer Uses

func NewServer(o ...Options) *Server

NewServer creates a new glue server instance. One variadic arguments specifies the server options.

func (*Server) Block Uses

func (s *Server) Block(b bool)

Block new incomming connections.

func (*Server) GetSocket Uses

func (s *Server) GetSocket(id string) *Socket

GetSocket obtains a socket by its ID. Returns nil if not found.

func (*Server) IsBlocked Uses

func (s *Server) IsBlocked() bool

IsBlocked returns a boolean whenever new incoming connections should be blocked.

func (*Server) OnNewSocket Uses

func (s *Server) OnNewSocket(f OnNewSocketFunc)

OnNewSocket sets the event function which is triggered if a new socket connection was made. The event function must not block! As soon as the event function returns, the socket is added to the active sockets map.

func (*Server) Release Uses

func (s *Server) Release()

Release this package. This will block all new incomming socket connections and close all current connected sockets.

func (*Server) Run Uses

func (s *Server) Run() error

Run starts the server and listens for incoming socket connections. This is a blocking method.

func (*Server) ServeHTTP Uses

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the HTTP Handler interface of the http package.

func (*Server) Sockets Uses

func (s *Server) Sockets() []*Socket

Sockets returns a list of all current connected sockets. Hint: Sockets are added to the active sockets list before the OnNewSocket event function is called. Use the IsInitialized flag to determind if a socket is not ready yet...

type Socket Uses

type Socket struct {
    // A Value is a placeholder for custom data.
    // Use this to attach socket specific data.
    Value interface{}
    // contains filtered or unexported fields
}

A Socket represents a single socket connections to a client.

func (*Socket) Channel Uses

func (s *Socket) Channel(name string) *Channel

Channel returns the corresponding channel value specified by the name. If no channel value exists for the given name, a new channel is created. Multiple calls to Channel with the same name, will always return the same channel value pointer.

func (*Socket) Close Uses

func (s *Socket) Close()

Close the socket connection.

func (*Socket) ClosedChan Uses

func (s *Socket) ClosedChan() ClosedChan

ClosedChan returns a channel which is non-blocking (closed) as soon as the socket is closed.

func (*Socket) DiscardRead Uses

func (s *Socket) DiscardRead()

DiscardRead ignores and discars the data received from the client. Call this method during initialization, if you don't read any data from the socket. If received data is not discarded, then the read buffer will block as soon as it is full, which will also block the keep-alive mechanism of the socket. The result would be a closed socket...

func (*Socket) ID Uses

func (s *Socket) ID() string

ID returns the socket's unique ID. This is a cryptographically secure pseudorandom number.

func (*Socket) IsClosed Uses

func (s *Socket) IsClosed() bool

IsClosed returns a boolean whenever the connection is closed.

func (*Socket) IsInitialized Uses

func (s *Socket) IsInitialized() bool

IsInitialized returns a boolean indicating if a socket is initialized and ready to be used. This flag is set to true after the OnNewSocket function has returned for this socket.

func (*Socket) OnClose Uses

func (s *Socket) OnClose(f OnCloseFunc)

OnClose sets the functions which is triggered if the socket connection is closed. This method can be called multiple times to bind multiple functions.

func (*Socket) OnRead Uses

func (s *Socket) OnRead(f OnReadFunc)

OnRead sets the function which is triggered if new data is received. If this event function based method of reading data from the socket is used, then don't use the socket Read method. Either use the OnRead or the Read approach.

func (*Socket) Read Uses

func (s *Socket) Read(timeout ...time.Duration) (string, error)

Read the next message from the socket. This method is blocking. One variadic argument sets a timeout duration. If no timeout is specified, this method will block forever. ErrSocketClosed is returned, if the socket connection is closed. ErrReadTimeout is returned, if the timeout is reached.

func (*Socket) RemoteAddr Uses

func (s *Socket) RemoteAddr() string

RemoteAddr returns the remote address of the client.

func (*Socket) UserAgent Uses

func (s *Socket) UserAgent() string

UserAgent returns the user agent of the client.

func (*Socket) Write Uses

func (s *Socket) Write(data string)

Write data to the client.

Directories

PathSynopsis
backendPackage backend provides the server backend with various socket implementations.
backend/closerEmit a close function only once, also if called multiple times.
backend/globalPackage global provides global types and constants for the backend packages.
backend/sockets/ajaxsocketPackage ajaxsocket provides the ajax socket implementation.
backend/sockets/websocket
logPackage log holds the log backend used by the socket library.
utilsPackage utils provides utilities for the glue socket implementation.

Package glue imports 15 packages (graph) and is imported by 7 packages. Updated 2019-06-21. Refresh now. Tools for package owners.