websocket

package
v0.0.0-...-9b5cd94 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package websocket contains implementation of websocket.

Index

Constants

View Source
const (
	RoomLoggedUserCount   = "logged-user-count"
	RoomBroadcastMessages = "broadcast-messages"

	RoomAlarmsGroup       = "alarms/"
	RoomAlarmDetailsGroup = "alarm-details/"

	RoomHealthcheck       = "healthcheck"
	RoomHealthcheckStatus = "healthcheck-status"
	RoomMessageRates      = "message-rates"
	RoomIcons             = "icons"
)
View Source
const (
	RMessageClientPing = iota
	RMessageJoin
	RMessageLeave
	RMessageAuth
)
View Source
const (
	WMessageClientPong = iota
	WMessageSuccess
	WMessageFail
	WMessageCloseRoom
	WMessageAuthSuccess
)

Variables

View Source
var ErrNotFoundRoom = errors.New("no room")
View Source
var ErrNotFoundRoomInGroup = errors.New("no group room")

Functions

This section is empty.

Types

type API

type API interface {
	Handler(c *gin.Context)
}

func NewApi

func NewApi(hub Hub) API

type Authorizer

type Authorizer interface {
	// Authenticate authenticates user by token.
	Authenticate(ctx context.Context, token string) (string, error)
	// Authorize checks if user has access to room.
	Authorize(ctx context.Context, userId, room string) (bool, error)
	// AddRoom adds room with permissions.
	AddRoom(room string, perms []string) error
	AddGroup(group string, perms []string, check GroupCheckExists) error
	GetGroupIds(group string) []string
	RemoveGroupRoom(group, id string) error
}

Authorizer is used to implement websocket room authentication and authorization.

func NewAuthorizer

func NewAuthorizer(
	enforcer security.Enforcer,
	tokenProviders []security.TokenProvider,
) Authorizer

type Connection

type Connection interface {
	WriteControl(messageType int, data []byte, deadline time.Time) error
	WriteJSON(v interface{}) error
	ReadJSON(v interface{}) error
	Close() error
	SetReadDeadline(t time.Time) error
	SetPongHandler(h func(string) error)
	RemoteAddr() net.Addr
}

type GroupCheckExists

type GroupCheckExists func(ctx context.Context, id string) (bool, error)

type GroupOnJoin

type GroupOnJoin func(ctx context.Context, connId, userId, id string, data any) error

type GroupOnLeave

type GroupOnLeave func(ctx context.Context, connId, id string) error

type GroupOnNotExist

type GroupOnNotExist func(ctx context.Context, id string) (any, error)

type GroupParameters

type GroupParameters struct {
	CheckExists GroupCheckExists
	OnNotExist  GroupOnNotExist
	OnJoin      GroupOnJoin
	OnLeave     GroupOnLeave
}

func (GroupParameters) IsZero

func (p GroupParameters) IsZero() bool

type Hub

type Hub interface {
	// Start pings connections.
	Start(ctx context.Context)
	// Connect creates listener connection.
	Connect(w http.ResponseWriter, r *http.Request) error
	// Send sends message to all listeners in room.
	Send(ctx context.Context, room string, msg any)
	SendGroupRoom(ctx context.Context, group, id string, msg any)
	SendGroupRoomByConnections(ctx context.Context, connIds []string, group, id string, b any)
	// RegisterRoom adds room with permissions.
	RegisterRoom(room string, perms ...string) error
	RegisterGroup(group string, params GroupParameters, perms ...string) error
	GetGroupIds(group string) []string
	GetConnectedGroupIds(group string) []string
	CloseGroupRoom(ctx context.Context, group, id string) error
	CloseGroupRoomAndNotify(ctx context.Context, group, id string) error
	GetUserTokens() []string
	GetConnections() []UserConnection
}

Hub interface is used to implement websocket room.

func NewHub

func NewHub(
	upgrader Upgrader,
	authorizer Authorizer,
	pingInterval time.Duration,
	logger zerolog.Logger,
) Hub

type RMessage

type RMessage struct {
	Type  int    `json:"type"`
	Room  string `json:"room"`
	Token string `json:"token"`
	Data  any    `json:"data"`
}

type Store

type Store interface {
	UpdateConnections(ctx context.Context, conns []UserConnection) error
	GetConnections(ctx context.Context, ids []string) (map[string]int64, error)
	GetActiveConnections(ctx context.Context) (int64, error)
	GetUsers(ctx context.Context) ([]string, error)
}

func NewStore

func NewStore(
	client mongo.DbClient,
	ttl time.Duration,
) Store

type Upgrader

type Upgrader interface {
	Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (Connection, error)
}

func NewUpgrader

func NewUpgrader(u websocket.Upgrader) Upgrader

type UserConnection

type UserConnection struct {
	ID     string
	UserID string
	Token  string
}

type WMessage

type WMessage struct {
	Type  int    `json:"type"`
	Room  string `json:"room,omitempty"`
	Msg   any    `json:"msg,omitempty"`
	Error int    `json:"error,omitempty"`
}

Jump to

Keyboard shortcuts

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