Documentation ¶
Index ¶
- Variables
- func AddClientToRoom(client *Client, roomId string)
- func Broadcast(roomId string, message []byte)
- func BroadcastByMeta(key string, value interface{}, message []byte)
- func BroadcastEvent(roomId string, event Event) error
- func BroadcastEventByMeta(key string, value interface{}, event Event)
- func BroadcastEventExcept(id string, event Event) error
- func BroadcastEventToAll(event Event) error
- func BroadcastEventToClient(id string, event Event) error
- func BroadcastExcept(id string, message []byte) error
- func BroadcastToAll(message []byte)
- func BroadcastToClient(id string, message []byte) error
- func GetClientRooms(client *Client) []string
- func GetConnectedClientIds() []string
- func GetConnectedClientIdsByMeta(key string, value interface{}) []string
- func GetConnectedClientIdsByRoom(roomId string) []string
- func IsEvent(data []byte) bool
- func IsJSONObject(data []byte) bool
- func RemoveClientFromAllRooms(client *Client)
- func RemoveClientFromRoom(client *Client, roomId string)
- type App
- func (a *App) AddHandshakeMiddleware(route string, middleware HandshakeMiddleware)
- func (a *App) AddReceiveMiddleware(route string, middleware ReceiveMiddleware)
- func (a *App) AddRouter(router *Router)
- func (a *App) AddSendMiddleware(route string, middleware SendMiddleware)
- func (a *App) ListenAndServe() error
- type Client
- func (c *Client) Close() error
- func (c *Client) GetID() string
- func (c *Client) GetMeta(key string) (interface{}, error)
- func (c *Client) GetRooms() []string
- func (c *Client) SetMeta(key string, value interface{})
- func (c *Client) Write(data []byte) error
- func (c *Client) WriteEvent(event Event) error
- func (c *Client) WriteJSON(data interface{}) error
- type ClientHandler
- func (ch *ClientHandler) On(event string, f func(client *Client, data []byte) error)
- func (ch *ClientHandler) OnConnect(f func(client *Client) error)
- func (ch *ClientHandler) OnDisconnect(f func(client *Client) error)
- func (ch *ClientHandler) OnEvent(event string, f func(client *Client, data interface{}) error)
- type ClientPool
- func (cp *ClientPool) AddClient(c *Client)
- func (cp *ClientPool) AddClientToRoom(c *Client, roomId string)
- func (cp *ClientPool) GetClient(id string) *Client
- func (cp *ClientPool) GetClients() map[string]*Client
- func (cp *ClientPool) GetClientsByMeta(key string, value interface{}) []*Client
- func (cp *ClientPool) GetRoom(roomId string) *Room
- func (cp *ClientPool) GetRooms() map[string]*Room
- func (cp *ClientPool) RemoveClient(c *Client)
- func (cp *ClientPool) RemoveClientFromAllRooms(c *Client, rooms []string)
- func (cp *ClientPool) RemoveClientFromRoom(c *Client, roomId string)
- func (cp *ClientPool) SendToAll(message []byte)
- func (cp *ClientPool) SendToAllByMeta(key string, value interface{}, message []byte)
- func (cp *ClientPool) SendToAllExcept(id string, message []byte)
- func (cp *ClientPool) SendToClient(id string, message []byte) error
- func (cp *ClientPool) SendToRoom(roomId string, message []byte)
- type Config
- type Event
- type HandlerFunc
- type HandshakeMiddleware
- type Payload
- type ReceiveMiddleware
- type Room
- type Route
- type Router
- type SendMiddleware
- type Server
Constants ¶
This section is empty.
Variables ¶
var ErrMetaNotFound = errors.New("metadata not found")
var (
ErrPubSubIsNil = errors.New("pub/sub client is nil")
)
Functions ¶
func AddClientToRoom ¶
AddClientToRoom adds a client to a room
func BroadcastByMeta ¶
BroadcastByMeta sends a Message to all clients with a specific metadata TODO: implement pub/sub
func BroadcastEvent ¶
BroadcastEvent sends an event to all clients in a room
func BroadcastEventByMeta ¶
BroadcastEventByMeta sends an event to all clients with a specific metadata TODO: implement pub/sub
func BroadcastEventExcept ¶
BroadcastEventExcept sends an event to all clients except the client with the given Id
func BroadcastEventToAll ¶
BroadcastEventToAll sends an event to all clients
func BroadcastEventToClient ¶
BroadcastEventToClient sends an event to a client with the given Id
func BroadcastExcept ¶
BroadcastExcept sends a Message to all clients except the client with the given id
func BroadcastToAll ¶
func BroadcastToAll(message []byte)
BroadcastToAll sends a Message to all clients connected
func BroadcastToClient ¶
BroadcastToClient sends a Message to a client with the given Id
func GetClientRooms ¶
GetClientRooms returns a list of all rooms the client is in
func GetConnectedClientIds ¶
func GetConnectedClientIds() []string
GetConnectedClientIds returns a list of all connected client ids
func GetConnectedClientIdsByMeta ¶
GetConnectedClientIdsByMeta returns a list of all connected client ids with a specific metadata
func IsJSONObject ¶
IsJSONObject checks if the data is JSON This uses the first and last character of the data to check if it is JSON and is not a 100% accurate way to check if the data is JSON (e.g. for an array) but is faster
func RemoveClientFromAllRooms ¶
func RemoveClientFromAllRooms(client *Client)
RemoveClientFromAllRooms removes a client from all rooms
func RemoveClientFromRoom ¶
RemoveClientFromRoom removes a client from a room
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
func (*App) AddHandshakeMiddleware ¶
func (a *App) AddHandshakeMiddleware(route string, middleware HandshakeMiddleware)
AddHandshakeMiddleware adds a middleware that is called before the websocket handshake only one per route allowed if multiple regex match the same route the first one will be used
func (*App) AddReceiveMiddleware ¶
func (a *App) AddReceiveMiddleware(route string, middleware ReceiveMiddleware)
AddReceiveMiddleware adds a middleware to the route regex (e.g. "/test" or "/test/:Id") multiple middlewares can be added to the same route (Caution: not ordered by adding order if multiple regex match the same route) Example: - "/test" will match "/test" - * will match everything
func (*App) AddSendMiddleware ¶
func (a *App) AddSendMiddleware(route string, middleware SendMiddleware)
AddSendMiddleware adds a middleware to the route regex (e.g. "/test" or ".*")
func (*App) ListenAndServe ¶
ListenAndServe starts the server and listens for incoming connections It will use TLS if the config.UseTLS is set to true and a cert and key are provided It will panic if no router is added (or for TLS no cert or key is provided)
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) SetMeta ¶
SetMeta adds metadata to a key value map Can be used to store data about the client (e.g. username, password, etc.) To get the metadata, use the GetMeta function
func (*Client) WriteEvent ¶
WriteEvent writes an event to the client as JSON
type ClientHandler ¶
type ClientHandler struct {
// contains filtered or unexported fields
}
func NewClientHandler ¶
func NewClientHandler() ClientHandler
func (*ClientHandler) On ¶
func (ch *ClientHandler) On(event string, f func(client *Client, data []byte) error)
On sets the on function
func (*ClientHandler) OnConnect ¶
func (ch *ClientHandler) OnConnect(f func(client *Client) error)
OnConnect sets the onConnect function
func (*ClientHandler) OnDisconnect ¶
func (ch *ClientHandler) OnDisconnect(f func(client *Client) error)
OnDisconnect sets the onDisconnect function
type ClientPool ¶
type ClientPool struct {
// contains filtered or unexported fields
}
func GetClientPool ¶
func GetClientPool() *ClientPool
func (*ClientPool) AddClient ¶
func (cp *ClientPool) AddClient(c *Client)
AddClient adds a client to the pool
func (*ClientPool) AddClientToRoom ¶
func (cp *ClientPool) AddClientToRoom(c *Client, roomId string)
AddClientToRoom adds a client to a Id
func (*ClientPool) GetClient ¶
func (cp *ClientPool) GetClient(id string) *Client
GetClient returns a client by Id
func (*ClientPool) GetClients ¶
func (cp *ClientPool) GetClients() map[string]*Client
GetClients returns all clients
func (*ClientPool) GetClientsByMeta ¶
func (cp *ClientPool) GetClientsByMeta(key string, value interface{}) []*Client
GetClientsByMeta returns all clients with a specific metadata
func (*ClientPool) GetRoom ¶
func (cp *ClientPool) GetRoom(roomId string) *Room
GetRoom returns a room by identifier
func (*ClientPool) GetRooms ¶
func (cp *ClientPool) GetRooms() map[string]*Room
GetRooms returns all rooms
func (*ClientPool) RemoveClient ¶
func (cp *ClientPool) RemoveClient(c *Client)
RemoveClient removes a client from the pool
func (*ClientPool) RemoveClientFromAllRooms ¶
func (cp *ClientPool) RemoveClientFromAllRooms(c *Client, rooms []string)
RemoveClientFromAllRooms removes a client from all rooms
func (*ClientPool) RemoveClientFromRoom ¶
func (cp *ClientPool) RemoveClientFromRoom(c *Client, roomId string)
RemoveClientFromRoom removes a client from a Id
func (*ClientPool) SendToAll ¶
func (cp *ClientPool) SendToAll(message []byte)
SendToAll sends a Message to all clients
func (*ClientPool) SendToAllByMeta ¶
func (cp *ClientPool) SendToAllByMeta(key string, value interface{}, message []byte)
SendToAllByMeta sends a Message to all clients with a specific metadata
func (*ClientPool) SendToAllExcept ¶
func (cp *ClientPool) SendToAllExcept(id string, message []byte)
SendToAllExcept sends a Message to all clients except the client with the given identifier
func (*ClientPool) SendToClient ¶
func (cp *ClientPool) SendToClient(id string, message []byte) error
SendToClient sends a Message to a client with the given Id
func (*ClientPool) SendToRoom ¶
func (cp *ClientPool) SendToRoom(roomId string, message []byte)
SendToRoom sends a Message to all clients in a Id
type Event ¶
type Event struct { // Event identifier used to identify the event on the client and server side // The ClientHandler.OnEvent() method uses this identifier to match the event Identifier string `json:"event"` // Data is the data that is sent with the event and can be of any type // On send and receive the data is converted from JSON to any type Data any `json:"data"` }
type HandlerFunc ¶
type HandlerFunc func(http.ResponseWriter, *http.Request)
type Route ¶
type Route struct { // Name of the route Path string // Handler function Handler ClientHandler }
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
func (*Router) AddRoute ¶
func (r *Router) AddRoute(path string, handler ClientHandler)
AddRoute adds a route to the router