Documentation ¶
Index ¶
- Constants
- Variables
- func DecodeMsgContent(r io.Reader, v any) error
- func NewControlBlock(cfg Config, options ...ControlBlockOption) (*controlBlock, error)
- func UnmarshalMsgContent(data []byte, v any) error
- type Broadcaster
- type Config
- type ConfigRequest
- type ContentMouseMove
- type ControlBlockOption
- type Controller
- type CreateSessionRequest
- type CreateSessionResponse
- type Dispatcher
- type GetConfigResponse
- type Message
- type Page
- type PageBackground
- type PageMeta
- type PageRequest
- type PageSize
- type PageSync
- type Stroke
- type Style
- type Textfield
- type User
- type UserRequest
Constants ¶
const ( MessageTypeSessionConfig = "config" MessageTypeStroke = "stroke" MessageTypeUserHost = "userhost" MessageTypeUserConnected = "userconn" MessageTypeUserSync = "usersync" MessageTypeUserDisconnected = "userdisc" MessageTypeUserKick = "userkick" MessageTypePageSync = "pagesync" MessageTypeMouseMove = "mmove" )
Message type definitions.
const (
QueryKeyUpdate = "update"
)
Variables ¶
var ( ErrReadyUser = errors.New("ready user not found") ErrUserConnected = errors.New("user already connected") ErrMaxUserReached = errors.New("maximum number of connected users reached") )
var ErrBroadcasterClosed = errors.New("broadcaster: closed")
Functions ¶
func DecodeMsgContent ¶
DecodeMsgContent is a shorthand wrapper to directly decode the content of generic API JSON messages.
func NewControlBlock ¶
func NewControlBlock(cfg Config, options ...ControlBlockOption) (*controlBlock, error)
NewControlBlock creates a new Session controlBlock with unique ID.
func UnmarshalMsgContent ¶
UnmarshalMsgContent is a shorthand wrapper to directly unmarshal the content of generic API JSON messages.
Types ¶
type Broadcaster ¶
type Broadcaster interface { // Bind binds the broadcaster to a session Bind(scb Controller) Broadcaster // Broadcast returns a channel for messages to be broadcasted Broadcast() chan<- Message // Send returns a channel for messages to sent to a specific client Send() chan<- Message // Control returns a channel for close messages to sent to a specific client Control() chan<- Message // Cache returns a channel for strokes to be stored in the cache Cache() chan<- []redis.Stroke // Close the broadcaster and cleans up all goroutines Close() }
func NewBroadcaster ¶
func NewBroadcaster(cache redis.Handler) Broadcaster
NewBroadcaster creates a new Broadcaster for a given session
type Config ¶
type Config struct { ID string `json:"id"` Host string `json:"host,omitempty"` Secret string `json:"-"` config.Session Password string `json:"password"` }
func (*Config) Update ¶
func (c *Config) Update(incoming *ConfigRequest) error
type ConfigRequest ¶
type ConfigRequest struct { MaxUsers opt.Option[int] `json:"maxUsers,omitempty"` ReadOnly opt.Option[bool] `json:"readOnly,omitempty"` Password opt.Option[string] `json:"password,omitempty"` }
func (*ConfigRequest) Validate ¶
func (c *ConfigRequest) Validate() error
type ContentMouseMove ¶
ContentMouseMove declares mouse move updates.
type ControlBlockOption ¶
type ControlBlockOption = func(scb *controlBlock)
func WithAttachments ¶
func WithAttachments(attachments attachment.Handler) ControlBlockOption
WithAttachments sets the attachment.Handler This functional argument is passed to NewControlBlock.
func WithBroadcaster ¶
func WithBroadcaster(broadcaster Broadcaster) ControlBlockOption
WithBroadcaster sets the Broadcaster This functional argument is passed to NewControlBlock.
func WithCache ¶
func WithCache(cache redis.Handler) ControlBlockOption
WithCache sets the redis.Handler This functional argument is passed to NewControlBlock.
func WithDispatcher ¶
func WithDispatcher(dispatcher Dispatcher) ControlBlockOption
WithDispatcher sets the Dispatcher This functional argument is passed to NewControlBlock.
type Controller ¶
type Controller interface { // ID returns the session id ID() string // Config returns the session config Config() Config // SetConfig sets the session config SetConfig(cfg *ConfigRequest) error // GetPageRank returns the current page rank of a session GetPageRank(ctx context.Context) ([]string, error) // GetPage returns a page from the session GetPage(ctx context.Context, pageId string, withStrokes bool) (*Page, error) // AddPages adds pages to the session AddPages(ctx context.Context, pageRequest PageRequest) error // UpdatePages perform an operation on the given pages. // Operations include: clear, delete and update meta data UpdatePages(ctx context.Context, pageRequest PageRequest, operation string) error // GetPageSync returns the page rank and all pages from the session (optionally with all strokes) GetPageSync(ctx context.Context, pageIds []string, withStrokes bool) (*PageSync, error) // SyncSession synchronizes the session with the given page rank and pages SyncSession(ctx context.Context, sync PageSync) error // IsValidPage checks if the given page ids are valid pages IsValidPage(ctx context.Context, pageID ...string) bool // NewUser creates a new ready user for the session NewUser(userReq UserRequest) (*User, error) // UpdateUser updates a user alias or color UpdateUser(user User, userReq UserRequest) error // UserCanJoin check if a user can join the session UserCanJoin(userID string) error // UserConnect connects a ready user to the session UserConnect(userID string, conn *gws.Conn) error // UserDisconnect disconnects a user from the session UserDisconnect(ctx context.Context, userID string) // KickUser removes a user from the session KickUser(userID string) error // GetUsers returns all active users in the session GetUsers() map[string]*User // Close closes a session Close() // CloseAfter closes a session after a specified timeout and executes fn CloseAfter(t time.Duration, fn func()) // Receive handles data received in the session Receive(ctx context.Context, msg *Message, userID string) error // Attachments returns the session's attachment handler Attachments() attachment.Handler // Broadcaster returns the session's broadcaster Broadcaster() Broadcaster // NumUsers returns the number of active users in the session NumUsers() int // Allow checks whether a user is allowed to modify the session Allow(userID string) bool }
type CreateSessionRequest ¶
type CreateSessionRequest struct {
ConfigRequest *ConfigRequest `json:"config,omitempty"`
}
type CreateSessionResponse ¶
type CreateSessionResponse struct {
Config Config `json:"config"`
}
type Dispatcher ¶
type Dispatcher interface { // GetSCB returns the session control block for given sessionID. GetSCB(sessionID string) (Controller, error) // Create creates and initializes a new SessionControl struct Create(ctx context.Context, cfg Config) (Controller, error) // Close removes the SCB from the active session map and closes the session. Close(sessionID string) error // IsValid checks if session with sessionID exists. IsValid(sessionID string) bool // NumSessions returns the number of active sessions NumSessions() int // NumUsers returns the number of active users in the session NumUsers() int }
func NewDispatcher ¶
func NewDispatcher(cache redis.Handler) Dispatcher
type GetConfigResponse ¶
type Message ¶
type Message struct { Type string `json:"type"` Sender string `json:"sender,omitempty"` Receiver string `json:"-"` Content any `json:"content,omitempty"` }
Message declares the generic message envelope of any API JSON encoded message.
func NewMessage ¶
NewMessage creates a new Message with any JSON encodable content, a message type and an optional sender.
func UnmarshalMessage ¶
UnmarshalMessage parses the JSON-encoded message and stores the result in the Message struct. The content field not parsed.
func (*Message) UnmarshalContent ¶
UnmarshalContent parses the JSON-encoded content of a Message and stores the result in the value pointed to by v.
type PageBackground ¶
type PageBackground struct { // page background Paper string `json:"paper,omitempty"` PageNum int `json:"documentPageNum"` AttachId string `json:"attachId"` }
PageStyle declares the style of the page background.
type PageMeta ¶
type PageMeta struct { PageSize PageSize `json:"size"` Background PageBackground `json:"background"` }
PageMeta declares some page meta data.
type PageRequest ¶
type PageRequest struct { PageID []string `json:"pageId"` Index []int `json:"index,omitempty"` Meta map[string]*PageMeta `json:"meta"` Strokes *map[string]map[string]*Stroke `json:"strokes,omitempty"` }
PageRequest declares the message content for page requests.
type Stroke ¶
type Stroke struct { Type int `json:"type"` ID string `json:"id,omitempty"` PageID string `json:"pageId,omitempty"` UserID string `json:"userId"` X float64 `json:"x"` Y float64 `json:"y"` ScaleX float64 `json:"scaleX,omitempty"` ScaleY float64 `json:"scaleY,omitempty"` Points []float64 `json:"points,omitempty"` Style Style `json:"style,omitempty"` Textfield Textfield `json:"textfield,omitempty"` }
Stroke declares the structure of most stoke types.
type Style ¶
type Style struct { Color string `json:"color"` Width float64 `json:"width"` Opacity float64 `json:"opacity"` }
Style declares the stroke style.
type Textfield ¶
type Textfield struct { Text string `json:"text"` Color string `json:"color"` HAlign string `json:"hAlign"` VAlign string `json:"vAlign"` Font string `json:"font"` FontWeight float64 `json:"fontWeight"` FontSize float64 `json:"fontSize"` LineHeight float64 `json:"lineHeight"` }
Textfield for editing richtext
type User ¶
type User struct { ID string `json:"id"` Alias string `json:"alias"` Color string `json:"color"` Conn *gws.Conn `json:"-"` }
User declares some information about connected users.