websocket

package
v0.20.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// WildcardNamespace is the namespace used as wildcard. It is used by listeners to filter callbacks.
	WildcardNamespace = "*"

	// ProtobufProtocol is used for protobuf encoded messages
	ProtobufProtocol = "protobuf"
	// JSONProtocol is used for JSON encoded messages
	JSONProtocol = "json"
)

Variables

View Source
var DefaultRequestTimeout = 10 * time.Second

DefaultRequestTimeout default timeout used for Request/Reply JSON message.

Functions

This section is empty.

Types

type Client

type Client struct {
	*Conn
	Path     string
	AuthOpts *shttp.AuthenticationOpts
	// contains filtered or unexported fields
}

Client is a outgoint client meaning a client connected to a remote websocket server. It embeds a Conn.

func NewClient

func NewClient(host string, clientType common.ServiceType, url *url.URL, authOpts *shttp.AuthenticationOpts, headers http.Header, queueSize int, writeCompression bool, tlsConfig *tls.Config) *Client

NewClient returns a Client with a new connection.

func (*Client) Connect

func (c *Client) Connect()

Connect to the server - and reconnect if necessary

func (*Client) UpgradeToStructSpeaker

func (c *Client) UpgradeToStructSpeaker() *StructSpeaker

type ClientPool

type ClientPool struct {
	*Pool
}

ClientPool is a pool of out going Speaker meaning connection to a remote Server.

func NewClientPool

func NewClientPool(name string) *ClientPool

NewClientPool returns a new ClientPool meaning a pool of outgoing Client.

func (*ClientPool) ConnectAll

func (s *ClientPool) ConnectAll()

ConnectAll calls connect to all the wSSpeakers of the pool.

type Conn

type Conn struct {
	common.RWMutex
	ConnStatus
	// contains filtered or unexported fields
}

Conn is the connection object of a Speaker

func (*Conn) AddEventHandler

func (c *Conn) AddEventHandler(h SpeakerEventHandler)

AddEventHandler registers a new event handler

func (*Conn) Connect

func (c *Conn) Connect()

Connect default implementation doing nothing as for incoming connection it is not used.

func (*Conn) Disconnect

func (c *Conn) Disconnect()

Disconnect the Speakers without waiting for termination.

func (*Conn) GetAddrPort

func (c *Conn) GetAddrPort() (string, int)

GetAddrPort returns the address and the port of the remote end.

func (*Conn) GetClientProtocol

func (c *Conn) GetClientProtocol() string

GetClientProtocol returns the websocket protocol.

func (*Conn) GetHeaders

func (c *Conn) GetHeaders() http.Header

GetHeaders returns the client HTTP headers.

func (*Conn) GetHost

func (c *Conn) GetHost() string

GetHost returns the hostname/host-id of the connection.

func (*Conn) GetRemoteHost

func (c *Conn) GetRemoteHost() string

GetRemoteHost returns the hostname/host-id of the remote side of the connection.

func (*Conn) GetRemoteServiceType

func (c *Conn) GetRemoteServiceType() common.ServiceType

GetRemoteServiceType returns the remote service type.

func (*Conn) GetServiceType

func (c *Conn) GetServiceType() common.ServiceType

GetServiceType returns the client type.

func (*Conn) GetStatus

func (c *Conn) GetStatus() ConnStatus

GetStatus returns the status of a WebSocket connection

func (*Conn) GetURL

func (c *Conn) GetURL() *url.URL

GetURL returns the URL of the connection

func (*Conn) IsConnected

func (c *Conn) IsConnected() bool

IsConnected returns the connection status.

func (*Conn) SendMessage

func (c *Conn) SendMessage(m Message) error

SendMessage adds a message to sending queue.

func (*Conn) SendRaw

func (c *Conn) SendRaw(b []byte) error

SendRaw adds raw bytes to sending queue.

type ConnState

type ConnState int32

ConnState describes the connection state

func (*ConnState) MarshalJSON

func (s *ConnState) MarshalJSON() ([]byte, error)

func (*ConnState) UnmarshalJSON

func (s *ConnState) UnmarshalJSON(b []byte) error

UnmarshalJSON deserialize a connection state

type ConnStatus

type ConnStatus struct {
	ServiceType       common.ServiceType
	ClientProtocol    string
	Addr              string
	Port              int
	Host              string      `json:"-"`
	State             *ConnState  `json:"IsConnected"`
	URL               *url.URL    `json:"-"`
	Headers           http.Header `json:"-"`
	ConnectTime       time.Time
	RemoteHost        string             `json:",omitempty"`
	RemoteServiceType common.ServiceType `json:",omitempty"`
}

WSConnStatus describes the status of a WebSocket connection

type DefaultSpeakerEventHandler

type DefaultSpeakerEventHandler struct {
}

DefaultSpeakerEventHandler implements stubs for the wsIncomingClientEventHandler interface

func (*DefaultSpeakerEventHandler) OnConnected

func (d *DefaultSpeakerEventHandler) OnConnected(c Speaker)

OnConnected is called when the connection is established.

func (*DefaultSpeakerEventHandler) OnDisconnected

func (d *DefaultSpeakerEventHandler) OnDisconnected(c Speaker)

OnDisconnected is called when the connection is closed or lost.

func (*DefaultSpeakerEventHandler) OnMessage

func (d *DefaultSpeakerEventHandler) OnMessage(c Speaker, m Message)

OnMessage is called when a message is received.

type IncomerHandler

type IncomerHandler func(*websocket.Conn, *auth.AuthenticatedRequest) Speaker

IncomerHandler incoming client handler interface.

type MasterElection

type MasterElection struct {
	common.RWMutex
	DefaultSpeakerEventHandler
	// contains filtered or unexported fields
}

MasterElection provides a mechanism based on etcd to elect a master from a SpeakerPool.

func NewMasterElection

func NewMasterElection(pool SpeakerPool) *MasterElection

NewMasterElection returns a new MasterElection.

func (*MasterElection) AddEventHandler

func (a *MasterElection) AddEventHandler(eventHandler MasterEventHandler)

AddEventHandler a new MasterEventHandler event handler.

func (*MasterElection) GetMaster

func (a *MasterElection) GetMaster() Speaker

GetMaster returns the current master.

func (*MasterElection) OnConnected

func (a *MasterElection) OnConnected(c Speaker)

OnConnected is triggered when a new Speaker get connected. If no master was elected this Speaker will be chosen as master.

func (*MasterElection) OnDisconnected

func (a *MasterElection) OnDisconnected(c Speaker)

OnDisconnected is triggered when a new Speaker get disconnected. If it was the master a new election is triggered.

func (*MasterElection) SendMessageToMaster

func (a *MasterElection) SendMessageToMaster(m Message)

SendMessageToMaster sends a message to the master.

type MasterEventHandler

type MasterEventHandler interface {
	OnNewMaster(c Speaker)
}

MasterEventHandler is the interface to be implemented by master election listeners.

type Message

type Message interface {
	Bytes(protocol string) []byte
}

Message is the interface of a message to send over the wire

type Pool

type Pool struct {
	common.RWMutex
	// contains filtered or unexported fields
}

Pool is a connection container. It embed a list of Speaker.

func (*Pool) AddClient

func (s *Pool) AddClient(c Speaker) error

AddClient adds the given Speaker to the pool.

func (*Pool) AddEventHandler

func (s *Pool) AddEventHandler(h SpeakerEventHandler)

AddEventHandler registers a new event handler.

func (*Pool) BroadcastMessage

func (s *Pool) BroadcastMessage(m Message)

BroadcastMessage broadcasts the given message.

func (*Pool) DisconnectAll

func (s *Pool) DisconnectAll()

DisconnectAll disconnects all the Speaker

func (*Pool) GetName

func (s *Pool) GetName() string

GetName returns the name of the pool

func (*Pool) GetSpeakerByRemoteHost

func (s *Pool) GetSpeakerByRemoteHost(host string) Speaker

GetSpeakerByRemoteHost returns the Speaker for the given remote host.

func (*Pool) GetSpeakers

func (s *Pool) GetSpeakers() (speakers []Speaker)

GetSpeakers returns the Speakers of the pool.

func (*Pool) GetSpeakersByType

func (s *Pool) GetSpeakersByType(serviceType common.ServiceType) (speakers []Speaker)

GetSpeakersByType returns Speakers matching the given type.

func (*Pool) GetStatus

func (s *Pool) GetStatus() map[string]ConnStatus

GetStatus returns the states of the WebSocket clients

func (*Pool) OnConnected

func (s *Pool) OnConnected(c Speaker)

OnConnected forwards the OnConnected event to event listeners of the pool.

func (*Pool) OnDisconnected

func (s *Pool) OnDisconnected(c Speaker)

OnDisconnected forwards the OnConnected event to event listeners of the pool.

func (*Pool) OnMessage

func (s *Pool) OnMessage(c Speaker, m Message)

OnMessage forwards the OnMessage event to event listeners of the pool.

func (*Pool) PickConnectedSpeaker

func (s *Pool) PickConnectedSpeaker() Speaker

PickConnectedSpeaker returns randomly a connected Speaker

func (*Pool) RemoveClient

func (s *Pool) RemoveClient(c Speaker) bool

RemoveClient removes client from the pool

func (*Pool) SendMessageTo

func (s *Pool) SendMessageTo(m Message, host string) error

SendMessageTo sends message to Speaker for the given remote host.

func (*Pool) Start

func (s *Pool) Start()

Start starts the pool in a goroutine.

func (*Pool) Stop

func (s *Pool) Stop()

Stop stops the pool and wait until stopped.

type RawMessage

type RawMessage []byte

RawMessage represents a raw message (array of bytes)

func (RawMessage) Bytes

func (m RawMessage) Bytes(protocol string) []byte

Bytes returns the string representation of the raw message

type Server

type Server struct {
	common.RWMutex
	// contains filtered or unexported fields
}

Server implements a websocket server. It owns a Pool of incoming Speakers.

func NewServer

func NewServer(server *shttp.Server, endpoint string, authBackend shttp.AuthenticationBackend, writeCompression bool, queueSize int, pingDelay, pongTimeout time.Duration) *Server

NewServer returns a new Server. The given auth backend will validate the credentials

func (Server) AddClient

func (s Server) AddClient(c Speaker) error

AddClient adds the given Speaker to the incomerPool.

func (Server) OnDisconnected

func (s Server) OnDisconnected(c Speaker)

OnDisconnected forwards the OnConnected event to event listeners of the pool.

type Speaker

type Speaker interface {
	GetStatus() ConnStatus
	GetHost() string
	GetAddrPort() (string, int)
	GetServiceType() common.ServiceType
	GetClientProtocol() string
	GetHeaders() http.Header
	GetURL() *url.URL
	IsConnected() bool
	SendMessage(m Message) error
	SendRaw(r []byte) error
	Connect()
	Disconnect()
	AddEventHandler(SpeakerEventHandler)
	GetRemoteHost() string
	GetRemoteServiceType() common.ServiceType
}

Speaker is the interface for a websocket speaking client. It is used for outgoing or incoming connections.

type SpeakerEventHandler

type SpeakerEventHandler interface {
	OnMessage(c Speaker, m Message)
	OnConnected(c Speaker)
	OnDisconnected(c Speaker)
}

SpeakerEventHandler is the interface to be implement by the client events listeners.

type SpeakerPool

type SpeakerPool interface {
	AddClient(c Speaker) error
	RemoveClient(c Speaker) bool
	AddEventHandler(h SpeakerEventHandler)
	GetSpeakers() []Speaker
	GetSpeakerByRemoteHost(host string) Speaker
	PickConnectedSpeaker() Speaker
	BroadcastMessage(m Message)
	SendMessageTo(m Message, host string) error
}

SpeakerPool is the interface that Speaker pools have to implement.

type SpeakerStructMessageDispatcher

type SpeakerStructMessageDispatcher interface {
	AddStructMessageHandler(h SpeakerStructMessageHandler, namespaces []string)
}

SpeakerStructMessageDispatcher interface is used to dispatch OnStructMessage events.

type SpeakerStructMessageHandler

type SpeakerStructMessageHandler interface {
	OnStructMessage(c Speaker, m *StructMessage)
}

SpeakerStructMessageHandler interface used to receive Struct messages.

type StructClientPool

type StructClientPool struct {
	*ClientPool
	// contains filtered or unexported fields
}

StructClientPool is a ClientPool able to send StructMessage.

func NewStructClientPool

func NewStructClientPool(name string) *StructClientPool

NewStructClientPool returns a new StructClientPool.

func (*StructClientPool) AddClient

func (a *StructClientPool) AddClient(c Speaker) error

AddClient adds a Client to the pool.

func (StructClientPool) AddStructMessageHandler

func (d StructClientPool) AddStructMessageHandler(h SpeakerStructMessageHandler, namespaces []string)

AddStructMessageHandler adds a new listener for Struct messages.

func (StructClientPool) AddStructSpeaker

func (d StructClientPool) AddStructSpeaker(c *StructSpeaker)

func (*StructClientPool) Request

func (s *StructClientPool) Request(host string, request *StructMessage, timeout time.Duration) (*StructMessage, error)

Request sends a Request Struct message to the Speaker of the given remote host.

type StructMessage

type StructMessage struct {
	Protocol  string
	Namespace string
	Type      string
	UUID      string
	Status    int64

	JsonObj *json.RawMessage

	ProtobufObj []byte
	// contains filtered or unexported fields
}

func NewStructMessage

func NewStructMessage(ns string, tp string, v interface{}, uuids ...string) *StructMessage

NewStructMessage creates a new StructMessage with the given namespace, type, value and optionally the UUID.

func (StructMessage) Bytes

func (g StructMessage) Bytes(protocol string) []byte

Bytes see Marshal

func (*StructMessage) Debug

func (g *StructMessage) Debug() string

Debug representation of the struct StructMessage

func (*StructMessage) DecodeObj

func (g *StructMessage) DecodeObj(obj interface{}) error

func (*StructMessage) Reply

func (g *StructMessage) Reply(v interface{}, kind string, status int) *StructMessage

Reply returns a reply message with the given value, type and status. Basically it return a new StructMessage with the correct Namespace and UUID.

func (*StructMessage) UnmarshalObj

func (g *StructMessage) UnmarshalObj(obj interface{}) error

type StructMessageJSON

type StructMessageJSON struct {
	Namespace string
	Type      string
	UUID      string
	Status    int64
	Obj       *json.RawMessage
}

func (*StructMessageJSON) Marshal

func (g *StructMessageJSON) Marshal() []byte

Marshal serializes the StructMessage into a JSON string.

type StructServer

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

StructServer is a Server able to handle StructSpeaker.

func NewStructServer

func NewStructServer(server *Server) *StructServer

NewStructServer returns a new StructServer

func (StructServer) AddClient

func (s StructServer) AddClient(c Speaker) error

AddClient adds the given Speaker to the incomerPool.

func (StructServer) AddStructMessageHandler

func (d StructServer) AddStructMessageHandler(h SpeakerStructMessageHandler, namespaces []string)

AddStructMessageHandler adds a new listener for Struct messages.

func (StructServer) AddStructSpeaker

func (d StructServer) AddStructSpeaker(c *StructSpeaker)

func (*StructServer) OnConnected

func (s *StructServer) OnConnected(c Speaker)

OnConnected websocket event.

func (*StructServer) OnDisconnected

func (s *StructServer) OnDisconnected(c Speaker)

OnDisconnected removes the Speaker from the incomer pool.

func (*StructServer) OnMessage

func (s *StructServer) OnMessage(c Speaker, m Message)

OnMessage websocket event.

func (*StructServer) Request

func (s *StructServer) Request(host string, request *StructMessage, timeout time.Duration) (*StructMessage, error)

Request sends a Request Struct message to the Speaker of the given remote host.

type StructSpeaker

type StructSpeaker struct {
	Speaker
	// contains filtered or unexported fields
}

StructSpeaker is a Speaker able to handle Struct Message and Request/Reply calls.

func (StructSpeaker) AddStructMessageHandler

func (a StructSpeaker) AddStructMessageHandler(h SpeakerStructMessageHandler, namespaces []string)

AddStructMessageHandler adds a new listener for Struct messages.

func (StructSpeaker) OnConnected

func (p StructSpeaker) OnConnected(c Speaker)

OnConnected is implemented here to avoid infinite loop since the default implemtation is triggering OnDisconnected too.

func (StructSpeaker) OnDisconnected

func (p StructSpeaker) OnDisconnected(c Speaker)

OnDisconnected is implemented here to avoid infinite loop since the default implemtation is triggering OnDisconnected too.

func (*StructSpeaker) OnMessage

func (s *StructSpeaker) OnMessage(c Speaker, m Message)

OnMessage checks that the Message comes from a StructSpeaker. It parses the Struct message and then dispatch the message to the proper listeners according to the namespace.

func (*StructSpeaker) Request

func (s *StructSpeaker) Request(m *StructMessage, timeout time.Duration) (*StructMessage, error)

Request sends a Struct message request waiting for a reply using the given timeout.

func (*StructSpeaker) Send

func (s *StructSpeaker) Send(m Message)

Send sends a message according to the namespace.

type StructSpeakerPool

type StructSpeakerPool interface {
	SpeakerPool
	SpeakerStructMessageDispatcher
	Request(host string, request *StructMessage, timeout time.Duration) (*StructMessage, error)
}

StructSpeakerPool is the interface of a pool of StructSpeakers.

Jump to

Keyboard shortcuts

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