Documentation ¶
Index ¶
- Variables
- type Client
- type ClientOpts
- type ClientPool
- type Conn
- func (c *Conn) AddEventHandler(h SpeakerEventHandler)
- func (c *Conn) Connect() error
- func (c *Conn) Flush()
- func (c *Conn) GetAddrPort() (string, int)
- func (c *Conn) GetClientProtocol() Protocol
- func (c *Conn) GetHeaders() http.Header
- func (c *Conn) GetHost() string
- func (c *Conn) GetRemoteHost() string
- func (c *Conn) GetRemoteServiceType() common.ServiceType
- func (c *Conn) GetServiceType() common.ServiceType
- func (c *Conn) GetStatus() ConnStatus
- func (c *Conn) GetURL() *url.URL
- func (c *Conn) IsConnected() bool
- func (c *Conn) Run()
- func (c *Conn) SendMessage(m Message) error
- func (c *Conn) SendRaw(b []byte) error
- func (c *Conn) Start()
- func (c *Conn) Stop()
- func (c *Conn) StopAndWait()
- type ConnState
- type ConnStatus
- type DefaultSpeakerEventHandler
- type IncomerHandler
- type MasterElection
- type MasterEventHandler
- type Message
- type Pool
- func (s *Pool) AddClient(c Speaker) error
- func (s *Pool) AddEventHandler(h SpeakerEventHandler)
- func (s *Pool) BroadcastMessage(m Message)
- func (s *Pool) DisconnectAll()
- func (s *Pool) GetName() string
- func (s *Pool) GetSpeakerByRemoteHost(host string) Speaker
- func (s *Pool) GetSpeakers() (speakers []Speaker)
- func (s *Pool) GetSpeakersByType(serviceType common.ServiceType) (speakers []Speaker)
- func (s *Pool) GetStatus() map[string]ConnStatus
- func (s *Pool) OnConnected(c Speaker)
- func (s *Pool) OnDisconnected(c Speaker)
- func (s *Pool) OnMessage(c Speaker, m Message)
- func (s *Pool) PickConnectedSpeaker() Speaker
- func (s *Pool) RemoveClient(c Speaker) bool
- func (s *Pool) SendMessageTo(m Message, host string) error
- func (s *Pool) Start()
- func (s *Pool) Stop()
- type PoolOpts
- type ProtobufObject
- type Protocol
- type RawMessage
- type Server
- type ServerOpts
- type Speaker
- type SpeakerEventHandler
- type SpeakerPool
- type SpeakerStructMessageDispatcher
- type SpeakerStructMessageHandler
- type StructClientPool
- func (s *StructClientPool) AddClient(c Speaker) error
- func (d StructClientPool) AddStructMessageHandler(h SpeakerStructMessageHandler, namespaces []string)
- func (d StructClientPool) AddStructSpeaker(c *StructSpeaker)
- func (s *StructClientPool) Request(host string, request *StructMessage, timeout time.Duration) (*StructMessage, error)
- type StructServer
- func (s StructServer) AddClient(c Speaker) error
- func (d StructServer) AddStructMessageHandler(h SpeakerStructMessageHandler, namespaces []string)
- func (d StructServer) AddStructSpeaker(c *StructSpeaker)
- func (s *StructServer) OnConnected(c Speaker)
- func (s *StructServer) OnDisconnected(c Speaker)
- func (s *StructServer) OnMessage(c Speaker, m Message)
- func (s *StructServer) Request(host string, request *StructMessage, timeout time.Duration) (*StructMessage, error)
- type StructSpeaker
- func (a StructSpeaker) AddStructMessageHandler(h SpeakerStructMessageHandler, namespaces []string)
- func (a StructSpeaker) OnConnected(c Speaker)
- func (a StructSpeaker) OnDisconnected(c Speaker)
- func (s *StructSpeaker) OnMessage(c Speaker, m Message)
- func (s *StructSpeaker) Request(m *StructMessage, timeout time.Duration) (*StructMessage, error)
- func (s *StructSpeaker) SendMessage(m Message) error
- type StructSpeakerPool
Constants ¶
This section is empty.
Variables ¶
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 TLSConfig *tls.Config Opts ClientOpts }
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, opts ClientOpts) *Client
NewClient returns a Client with a new connection.
func (*Client) Start ¶
func (c *Client) Start()
Start connects to the server - and reconnect if necessary
func (*Client) UpgradeToStructSpeaker ¶
func (c *Client) UpgradeToStructSpeaker() *StructSpeaker
UpgradeToStructSpeaker a WebSocket client to a StructSpeaker
type ClientOpts ¶
type ClientOpts struct { Protocol Protocol AuthOpts *shttp.AuthenticationOpts Headers http.Header QueueSize int WriteCompression bool TLSConfig *tls.Config Logger logging.Logger }
ClientOpts defines some options that can be set when creating a new client
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, opts PoolOpts) *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 ¶
Connect default implementation doing nothing as for incoming connection it is not used.
func (*Conn) GetAddrPort ¶
GetAddrPort returns the address and the port of the remote end.
func (*Conn) GetClientProtocol ¶
GetClientProtocol returns the websocket protocol.
func (*Conn) GetHeaders ¶
GetHeaders returns the client HTTP headers.
func (*Conn) GetRemoteHost ¶
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) IsConnected ¶
IsConnected returns the connection status.
func (*Conn) SendMessage ¶
SendMessage adds a message to sending queue.
func (*Conn) StopAndWait ¶
func (c *Conn) StopAndWait()
StopAndWait disconnect the speaker and wait for the goroutine to end
type ConnState ¶
type ConnState common.ServiceState
ConnState describes the connection state
func (*ConnState) CompareAndSwap ¶
func (s *ConnState) CompareAndSwap(old, new common.ServiceState) bool
CompareAndSwap executes the compare-and-swap operation for a state
func (*ConnState) Load ¶
func (s *ConnState) Load() common.ServiceState
Load atomatically loads and returns the state
func (*ConnState) MarshalJSON ¶
MarshalJSON marshal the connection state to JSON
func (*ConnState) Store ¶
func (s *ConnState) Store(state common.ServiceState)
Store atomatically stores the state
func (*ConnState) UnmarshalJSON ¶
UnmarshalJSON de-serialize a connection state
type ConnStatus ¶
type ConnStatus struct { ServiceType common.ServiceType ClientProtocol Protocol 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"` }
ConnStatus 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, clientPromoter) (Speaker, error)
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 Pool ¶
Pool is a connection container. It embed a list of Speaker.
func (*Pool) AddEventHandler ¶
func (s *Pool) AddEventHandler(h SpeakerEventHandler)
AddEventHandler registers a new event handler.
func (*Pool) BroadcastMessage ¶
BroadcastMessage broadcasts the given message.
func (*Pool) DisconnectAll ¶
func (s *Pool) DisconnectAll()
DisconnectAll disconnects all the Speaker
func (*Pool) GetSpeakerByRemoteHost ¶
GetSpeakerByRemoteHost returns the Speaker for the given remote host.
func (*Pool) GetSpeakers ¶
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 ¶
OnConnected forwards the OnConnected event to event listeners of the pool.
func (*Pool) OnDisconnected ¶
OnDisconnected forwards the OnConnected event to event listeners of the pool.
func (*Pool) PickConnectedSpeaker ¶
PickConnectedSpeaker returns randomly a connected Speaker
func (*Pool) RemoveClient ¶
RemoveClient removes client from the pool
func (*Pool) SendMessageTo ¶
SendMessageTo sends message to Speaker for the given remote host.
type ProtobufObject ¶
ProtobufObject defines an object that can be serialized in protobuf
type Protocol ¶
type Protocol string
Protocol used to transport messages
const ( // WildcardNamespace is the namespace used as wildcard. It is used by listeners to filter callbacks. WildcardNamespace = "*" // RawProtocol is used for raw messages RawProtocol Protocol = "raw" // ProtobufProtocol is used for protobuf encoded messages ProtobufProtocol Protocol = "protobuf" // JSONProtocol is used for JSON encoded messages JSONProtocol Protocol = "json" )
type Server ¶
Server implements a websocket server. It owns a Pool of incoming Speakers.
func NewServer ¶
func NewServer(server *shttp.Server, endpoint string, authBackend shttp.AuthenticationBackend, opts ServerOpts) *Server
NewServer returns a new Server. The given auth backend will validate the credentials
func (Server) OnDisconnected ¶
func (s Server) OnDisconnected(c Speaker)
OnDisconnected forwards the OnConnected event to event listeners of the pool.
type ServerOpts ¶
type ServerOpts struct { WriteCompression bool QueueSize int PingDelay time.Duration PongTimeout time.Duration Logger logging.Logger }
ServerOpts defines server options
type Speaker ¶
type Speaker interface { GetStatus() ConnStatus GetHost() string GetAddrPort() (string, int) GetServiceType() common.ServiceType GetClientProtocol() Protocol GetHeaders() http.Header GetURL() *url.URL IsConnected() bool SendMessage(m Message) error SendRaw(r []byte) error Connect() error Start() Stop() StopAndWait() 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, opts PoolOpts) *StructClientPool
NewStructClientPool returns a new StructClientPool.
func (*StructClientPool) AddClient ¶
func (s *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)
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) 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.
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 (a StructSpeaker) OnConnected(c Speaker)
OnConnected is implemented here to avoid infinite loop since the default implementation is triggering OnDisconnected too.
func (StructSpeaker) OnDisconnected ¶
func (a StructSpeaker) OnDisconnected(c Speaker)
OnDisconnected is implemented here to avoid infinite loop since the default implementation 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) SendMessage ¶
func (s *StructSpeaker) SendMessage(m Message) error
SendMessage 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.