...

Package group

import "github.com/Diarkis/diarkis/group"
Overview
Index

Overview ▾

Package group ▷

Group - Distributed MO

Suited for building chat and similar functionalities. Able to connect users with different network protocols with no limit to the number of users to connect with.

Configurations

Group configurations are explained below.

{
  "relayLimit": 2,
  "sendInterval": 300,
  "staticGroups": [
    { "groupID": "52 character long uniqe groupd ID", "interval": 200 }
  ]
}

▶︎ Configuration Value

relayLimit - Default is 2

Defines the number of mesh nodes to send message at a time when broadcasting messages. Default is 2. Higher the number, lower the latency and higher to CPU load.

sendInterval - Default is 300

Defines broadcast packet send interval in milliseconds. Increasing this number will cause more delay, but less CPU stress.

staticGroups - A list of static groups to create on process start. Group ID can be custom defined. Interval controls message bufferring on the server.

Static groups are statcially created on server process start and will never be discarded until the server process stops. Useful for creating a group that expects all clients to join for global notifications etc.

func AfterBroadcastGroupCmd

func AfterBroadcastGroupCmd(callback func(*user.User, func(error)))

AfterBroadcastGroupCmd registers a callback function to be executed after broadcast group command: Must be called before ExposeCommands

func AfterCreateGroupCmd

func AfterCreateGroupCmd(callback func(*user.User, func(error)))

AfterCreateGroupCmd registers a callback function to be executed after create group command: Must be called before ExposeCommands

func AfterJoinGroupCmd

func AfterJoinGroupCmd(callback func(*user.User, func(error)))

AfterJoinGroupCmd registers a callback function to be executed after join group command: Must be called before ExposeCommands

func AfterLeaveGroupCmd

func AfterLeaveGroupCmd(callback func(*user.User, func(error)))

AfterLeaveGroupCmd registers a callback function to be executed after leave group command: Must be called before ExposeCommands

func Announce

func Announce(groupID string, ver uint8, cmd uint16, message []byte, reliable bool)

Announce sends a broadcast message to the other members in the group without having the "sender" - MUST not be called by client directly for security reason

[NOTE] Uses mutex lock internally.

[NOTE] This function is asynchronous.

groupID string - Target group ID.

senderUSer *user.User - User that sends the broadcast.

ver uint8 - Command version used for the broadcast message.

cmd uint16 - Command ID used for the broadcast message.

message []byte - Message byte array to be sent as the braodcast.

reliable bool - If true, UDP will become RUDP.

func BeforeBroadcastGroupCmd

func BeforeBroadcastGroupCmd(callback func(*user.User, func(error)))

BeforeBroadcastGroupCmd registers a callback function to be executed before broadcast group command: Must be called before ExposeCommands

func BeforeCreateGroupCmd

func BeforeCreateGroupCmd(callback func(*user.User, func(error)))

BeforeCreateGroupCmd registers a callback function to be executed before create group command: Must be called before ExposeCommands

func BeforeJoinGroupCmd

func BeforeJoinGroupCmd(callback func(*user.User, func(error)))

BeforeJoinGroupCmd registers a callback function to be executed before join group command: Must be called before ExposeCommands

func BeforeLeaveGroupCmd

func BeforeLeaveGroupCmd(callback func(*user.User, func(error)))

BeforeLeaveGroupCmd registers a callback function to be executed before leave group command: Must be called before ExposeCommands

func Broadcast

func Broadcast(groupID string, senderUser *user.User, ver uint8, cmd uint16, message []byte, reliable bool)

Broadcast Sends a broadcast message to the other members in the group

[NOTE] Uses mutex lock internally.

[NOTE] This function is asynchronous.

groupID string - Target group ID.

senderUSer *user.User - User that sends the broadcast.

ver uint8 - Command version used for the broadcast message.

cmd uint16 - Command ID used for the broadcast message.

message []byte - Message byte array to be sent as the braodcast.

reliable bool - If true, UDP will become RUDP.

func ExposeCommands

func ExposeCommands()

ExposeCommands exposes commands to the client to work with the Group package

func GetGroupIDList

func GetGroupIDList(userData *user.User) []string

GetGroupIDList returns an array of group IDs

[NOTE] Uses mutex lock internally.

userData *user.User - Target user to retrieve the list of group IDs from.

func GetLatestGroupID

func GetLatestGroupID(userData *user.User) string

GetLatestGroupID returns the newest group ID the user joined or created

[NOTE] Uses mutex lock internally.

userData *user.User - Target user to retrieve the last group ID joined.

func GetMemberMeshNodes

func GetMemberMeshNodes(groupID string) []string

GetMemberMeshNodes returns an array of member mesh nodes that are participating the group

[NOTE] Uses mutex lock internally.

groupID string - Target group ID to retrieve the list of server internal addresses that group is stored.

func Join

func Join(groupID string, userData *user.User, ver uint8, cmd uint16, message []byte, callback func(error))

Join Joins a group and notify the other members of the group on joining the group If message is nil or empty, broadcast will NOT be sent

[NOTE] Uses mutex lock internally.

[NOTE] This function is asynchronous.

groupID string - Target group ID to join.

userData *user.User - User to join the group.

ver uint8 - Command version used for the message sent when join is successful.

cmd uint16 - Command ID used for the message sent when join is successful.

message []byte - Message byte array to be sent when join is successful.
If message is either nil or empty, the message will not be sent.

callback func(error) - Callback invoked when join operation is completed.

func Leave

func Leave(groupID string, userData *user.User, ver uint8, cmd uint16, message []byte, callback func(error))

Leave Leaves from a group and notify the other members on leaving If message is nil or empty, broadcast will NOT be sent

[NOTE] Uses mutex lock internally.

[NOTE] This function is asynchronous.

groupID string - Target group ID to leave from.

userData *user.User - User to leave the group.

ver uint8 - Command version used for the message sent when leave is successful.

cmd uint16 - Command ID used for the message sent when leave is successful.

message []byte - Message byte array to be sent when leave is successful.
If message is either nil or empty, the message will not be sent.

callback func(error) - Callback invoked when join operation is completed.

func NewGroup

func NewGroup(userData *user.User, allowEmpty bool, join bool, ttl int64, interval int64) (string, error)

NewGroup Creates a new group and returns the groupp ID as a string to the callback function.

[NOTE] Uses mutex lock internally.

allowEmpty - If true, the group will not be deleted until TTL expires even if all members leave
join       - If true, the creator user will join the group automatically
ttl        - TTL of the group in seconds. The value must be greater than 0
interval   - Broadcast interval in milliseconds. interval below 100ms will be forced to 0. If it's 0, no packet merge

func SetOnDiscardCustomMessage

func SetOnDiscardCustomMessage(custom func(string, string, string) []byte)

SetOnDiscardCustomMessage sets a custom function to create a custom message to be sent to members on user discard

custom func(groupID string, userID string, userSID string) []byte - Custom function to create custom message byte array.

func SetOnRejoinCustomMessage

func SetOnRejoinCustomMessage(custom func(string, string, string) []byte)

SetOnRejoinCustomMessage assigns a callback on rejoin to create a custom message

custom func(groupID string, userID string, userSID string) []byte - Custom function to create custom message byte array.

func Setup

func Setup(confpath string)

Setup optionally loads configurations

configpath string - Absolute path for the configuration file to read.

func SetupAsTCPServer

func SetupAsTCPServer()

SetupAsTCPServer Sets up group package as TCP server

func SetupAsUDPServer

func SetupAsUDPServer()

SetupAsUDPServer Sets up group package as UDP/RUDP server

func SetupAsWebSocketServer

func SetupAsWebSocketServer()

SetupAsWebSocketServer Sets up group package as WebSocket server

type Group

Group group data structure

type Group struct {
    ID         string
    AllowEmpty bool
    Members    []*Member
    // remote member mesh node list
    RemoteMemberNodes map[string]bool
    // user ID as key
    MemberMeshEndPoints map[string]string
    // user ID as key and user SID as value
    MemberSIDs map[string]string
    // If true, the group is static and never discarded
    IsStatic bool

    TTL int64
    // contains filtered or unexported fields
}

type Member

Member member of a group data structure

type Member struct {
    UID          string
    SID          string
    MeshEndPoint string
}