server

package
v0.0.0-...-5b28944 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: Apache-2.0 Imports: 15 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// ErrorInvalidKey Invalid key provided
	ErrorInvalidKey = "Invalid key provided"
	// ErrorInvalidToken Invalid token provided
	ErrorInvalidToken = "Invalid token provided"
	// ErrorInvalidWSParameters No id, token, or key supplied to websocket server
	ErrorInvalidWSParameters = "No id, token, or key supplied to websocket server"
	// ErrorConnectionLimitExceeded Server has reached its concurrent user limit
	ErrorConnectionLimitExceeded = "Server has reached its concurrent user limit"
	// MessageTypeOpen OPEN
	MessageTypeOpen = "OPEN"
	// MessageTypeLeave LEAVE
	MessageTypeLeave = "LEAVE"
	// MessageTypeCandidate CANDIDATE
	MessageTypeCandidate = "CANDIDATE"
	// MessageTypeOffer OFFER
	MessageTypeOffer = "OFFER"
	// MessageTypeAnswer ANSWER
	MessageTypeAnswer = "ANSWER"
	// MessageTypeExpire EXPIRE
	MessageTypeExpire = "EXPIRE"
	// MessageTypeHeartbeat HEARTBEAT
	MessageTypeHeartbeat = "HEARTBEAT"
	// MessageTypeIDTaken ID-TAKEN
	MessageTypeIDTaken = "ID-TAKEN"
	// MessageTypeError ERROR
	MessageTypeError = "ERROR"

	// WebsocketEventMessage message
	WebsocketEventMessage = "message"
	// WebsocketEventConnection connection
	WebsocketEventConnection = "connection"
	// WebsocketEventError error
	WebsocketEventError = "error"
	// WebsocketEventClose close
	WebsocketEventClose = "close"
)
View Source
const DefaultCheckInterval = 300

Variables

View Source
var ClientIDGenerator = func() string {
	return util.RandomToken()
}

ClientIDGenerator default hash generator

Functions

func NewHeartbeatHandler

func NewHeartbeatHandler(opts Options) func(client IClient, message models.IMessage) bool

NewHeartbeatHandler handles a heartbeat

func NewTransmissionHandler

func NewTransmissionHandler(realm IRealm, opts Options) func(client IClient, message models.IMessage) bool

NewTransmissionHandler handles transmission of messages

Types

type Auth

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

Auth handles request authentication

func NewAuth

func NewAuth(realm IRealm, opts Options) *Auth

NewAuth init a new Auth middleware

func (*Auth) HTTPHandler

func (a *Auth) HTTPHandler(handler http.HandlerFunc) http.Handler

HTTPHandler return an HTTP handler middleware

func (*Auth) WSHandler

func (a *Auth) WSHandler(handler http.HandlerFunc) http.HandlerFunc

WSHandler return a websocket handler middleware

type AuthError

type AuthError struct {
	Err        error
	StatusCode int
}

AuthError is an error that occurs during authentication and contains the original error plus the http status code that should be returned to the client

func (AuthError) Error

func (e AuthError) Error() string

type CheckBrokenConnections

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

CheckBrokenConnections watch for broken connections

func NewCheckBrokenConnections

func NewCheckBrokenConnections(realm IRealm, opts Options, onClose func(client IClient)) *CheckBrokenConnections

NewCheckBrokenConnections create a new CheckBrokenConnections

func (*CheckBrokenConnections) Start

func (b *CheckBrokenConnections) Start()

Start initialize the connection checker

func (*CheckBrokenConnections) Stop

func (b *CheckBrokenConnections) Stop()

Stop close the connection checker

type Client

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

Client implementation

func NewClient

func NewClient(id string, token string) *Client

NewClient initialize a new client

func (*Client) GetID

func (c *Client) GetID() string

GetID return client id

func (*Client) GetLastPing

func (c *Client) GetLastPing() int64

GetLastPing return the last ping timestamp

func (*Client) GetSocket

func (c *Client) GetSocket() *websocket.Conn

GetSocket return the web socket server

func (*Client) GetToken

func (c *Client) GetToken() string

GetToken return client token

func (*Client) Send

func (c *Client) Send(data []byte) error

Send send data

func (*Client) SetLastPing

func (c *Client) SetLastPing(lastPing int64)

SetLastPing set last ping timestamp

func (*Client) SetSocket

func (c *Client) SetSocket(socket *websocket.Conn)

SetSocket set the web socket handler

type ClientMessage

type ClientMessage struct {
	Client  IClient
	Message *models.Message
}

ClientMessage wrap a message received by a client

type HTTPServer

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

HTTPServer peer server

func NewHTTPServer

func NewHTTPServer(realm IRealm, auth *Auth, wss *WebSocketServer, opts Options) *HTTPServer

NewHTTPServer init a server

func (*HTTPServer) Start

func (h *HTTPServer) Start() error

Start start the HTTP server

func (*HTTPServer) StartTLS

func (h *HTTPServer) StartTLS(certFile, keyFile string) error

Start start the HTTPS server

func (*HTTPServer) Stop

func (h *HTTPServer) Stop() error

Stop stops the HTTP server

type Handler

type Handler func(client IClient, message models.IMessage) bool

Handler wrap a callback

type HandlersRegistry

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

HandlersRegistry handlers registry

func (*HandlersRegistry) Handle

func (r *HandlersRegistry) Handle(client IClient, message models.IMessage) bool

Handle handles a message

func (*HandlersRegistry) RegisterHandler

func (r *HandlersRegistry) RegisterHandler(messageType string, handler Handler)

RegisterHandler register an handler

type IClient

type IClient interface {
	GetID() string
	GetToken() string
	GetSocket() *websocket.Conn
	SetSocket(socket *websocket.Conn)
	GetLastPing() int64
	SetLastPing(lastPing int64)
	Send(data []byte) error
}

IClient client interface

type IHandlersRegistry

type IHandlersRegistry interface {
	RegisterHandler(messageType string, handler Handler)
	Handle(client IClient, message models.IMessage) bool
}

IHandlersRegistry interface for HandlersRegistry

func NewHandlersRegistry

func NewHandlersRegistry() IHandlersRegistry

NewHandlersRegistry creates a new HandlersRegistry

type IMessageHandler

type IMessageHandler interface {
	Handle(client IClient, message models.IMessage) bool
}

IMessageHandler interface for MessageHandler

type IMessageQueue

type IMessageQueue interface {
	GetLastReadAt() int64
	AddMessage(message models.IMessage)
	ReadMessage() models.IMessage
	GetMessages() []models.IMessage
}

IMessageQueue message queue interface

type IMessagesExpire

type IMessagesExpire interface {
	Start()
	Stop()
}

IMessagesExpire MessagesExpire interface

type IRealm

type IRealm interface {
	GetClientsIds() []string
	GetClientByID(clientID string) IClient
	GetClientsIdsWithQueue() []string
	SetClient(client IClient, id string)
	RemoveClientByID(id string) bool
	GetMessageQueueByID(id string) IMessageQueue
	AddMessageToQueue(id string, message models.IMessage)
	ClearMessageQueue(id string)
	GenerateClientID() string
}

IRealm interface for Realm

type MessageHandler

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

MessageHandler wrap the message handler

func NewMessageHandler

func NewMessageHandler(realm IRealm, handlersRegistry IHandlersRegistry, opts Options) *MessageHandler

NewMessageHandler creates a new MessageHandler

func (*MessageHandler) Handle

func (m *MessageHandler) Handle(client IClient, message models.IMessage) bool

Handle handles a message

type MessageQueue

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

MessageQueue type

func NewMessageQueue

func NewMessageQueue() *MessageQueue

NewMessageQueue creates a new MessageQueue

func (*MessageQueue) AddMessage

func (mq *MessageQueue) AddMessage(message models.IMessage)

AddMessage add message to queue

func (*MessageQueue) GetLastReadAt

func (mq *MessageQueue) GetLastReadAt() int64

GetLastReadAt return last message read time

func (*MessageQueue) GetMessages

func (mq *MessageQueue) GetMessages() []models.IMessage

GetMessages return all queued messages

func (*MessageQueue) ReadMessage

func (mq *MessageQueue) ReadMessage() models.IMessage

ReadMessage read last message

type MessagesExpire

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

MessagesExpire check for expired messages

func NewMessagesExpire

func NewMessagesExpire(realm IRealm, opts Options, messageHandler IMessageHandler) *MessagesExpire

func (*MessagesExpire) Start

func (b *MessagesExpire) Start()

Start the message expire check

func (*MessagesExpire) Stop

func (b *MessagesExpire) Stop()

Stop the message expire check

type Options

type Options struct {
	Port            int
	Host            string
	LogLevel        string
	ExpireTimeout   int64
	AliveTimeout    int64
	Key             string
	Path            string
	ConcurrentLimit int
	AllowDiscovery  bool
	CleanupOutMsgs  int
}

Options peer server options

func NewOptions

func NewOptions() Options

NewOptions create default options

type PeerServer

type PeerServer struct {
	emitter.Emitter
	// contains filtered or unexported fields
}

PeerServer wrap the peer server functionalities

func New

func New(opts Options) *PeerServer

New creates a new PeerServer

func (*PeerServer) Start

func (p *PeerServer) Start() error

Start start the peer server

func (*PeerServer) StartTLS

func (p *PeerServer) StartTLS(certFile, keyFile string) error

Start start the TLS peer server

func (*PeerServer) Stop

func (p *PeerServer) Stop() error

Stop stops the peer server

type Realm

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

Realm implementation of a realm

func NewRealm

func NewRealm() *Realm

NewRealm creates a new Realm

func (*Realm) AddMessageToQueue

func (r *Realm) AddMessageToQueue(id string, message models.IMessage)

AddMessageToQueue add message to queue

func (*Realm) ClearMessageQueue

func (r *Realm) ClearMessageQueue(id string)

ClearMessageQueue clear message queue

func (*Realm) GenerateClientID

func (r *Realm) GenerateClientID() string

GenerateClientID generate a client id

func (*Realm) GetClientByID

func (r *Realm) GetClientByID(clientID string) IClient

GetClientByID return client by id

func (*Realm) GetClientsIds

func (r *Realm) GetClientsIds() []string

GetClientsIds return the list of client id

func (*Realm) GetClientsIdsWithQueue

func (r *Realm) GetClientsIdsWithQueue() []string

GetClientsIdsWithQueue retur clients with queue

func (*Realm) GetMessageQueueByID

func (r *Realm) GetMessageQueueByID(id string) IMessageQueue

GetMessageQueueByID get message by queue id

func (*Realm) RemoveClientByID

func (r *Realm) RemoveClientByID(id string) bool

RemoveClientByID remove a client by id

func (*Realm) SetClient

func (r *Realm) SetClient(client IClient, id string)

SetClient set a client

type WebSocketServer

type WebSocketServer struct {
	emitter.Emitter
	// contains filtered or unexported fields
}

WebSocketServer wrap the websocket server

func NewWebSocketServer

func NewWebSocketServer(realm IRealm, opts Options) *WebSocketServer

NewWebSocketServer create a new WebSocketServer

func (*WebSocketServer) Handler

func (wss *WebSocketServer) Handler() http.HandlerFunc

Handler expose the http handler for websocket

func (*WebSocketServer) Send

func (wss *WebSocketServer) Send(data []byte)

Send send data to the clients

Jump to

Keyboard shortcuts

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