func AfterBroadcastRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
AfterBroadcastRoomCmd registers a callback function to be executed after broadcast room command: Must be called before ExposeCommands
func AfterCreateRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
AfterCreateRoomCmd registers a callback function to be executed after create room command: Must be called before ExposeCommands
func AfterGetRoomPropCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
AfterGetRoomPropCmd registers a callback function to be executed after get room properties: Must be called before ExposeCommands
func AfterJoinRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
AfterJoinRoomCmd registers a callback function to be executed after join room command: Must be called before ExposeCommands
func AfterLeaveRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
AfterLeaveRoomCmd registers a callback function to be executed after leave room command: Must be called before ExposeCommands
func AfterMessageRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
AfterMessageRoomCmd registers a callback function to be executed after message room command: Must be called before ExposeCommands
func AfterUpdateRoomPropCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
AfterUpdateRoomPropCmd registers a callback function to be executed after update room properties: Must be called before ExposeCommands
func Announce(roomID string, memberIDs []string, ver uint8, cmd uint16, message []byte, reliable bool)
Announce Sends a message to selected members to the room without having a "sender" - This function must NOT be called by user
func BeforeBroadcastRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
BeforeBroadcastRoomCmd registers a callback function to be executed before broadcast room command: Must be called before ExposeCommands
func BeforeCreateRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
BeforeCreateRoomCmd registers a callback function to be executed before create room command: Must be called before ExposeCommands
func BeforeGetRoomPropCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
BeforeGetRoomPropCmd registers a callback function to be executed before get room properties: Must be called before ExposeCommands
func BeforeJoinRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
BeforeJoinRoomCmd registers a callback function to be executed before join room command: Must be called before ExposeCommands
func BeforeLeaveRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
BeforeLeaveRoomCmd registers a callback function to be executed before leave room command: Must be called before ExposeCommands
func BeforeMessageRoomCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
BeforeMessageRoomCmd registers a callback function to be executed before message room command: Must be called before ExposeCommands
func BeforeUpdateRoomPropCmd(callback func(uint8, uint16, []byte, *user.User, func(error)))
BeforeUpdateRoomPropCmd registers a callback function to be executed before update room properties: Must be called before ExposeCommands
func Broadcast(roomID string, senderUser *user.User, ver uint8, cmd uint16, message []byte, reliable bool)
Broadcast Sends a broadcast message to the other members in the room
func CancelReservation(roomID string, userData *user.User, memberIDs []string) error
CancelReservation removes reservation per members
Only the room owner is permitted to run this operation
func ChangeTTL(roomID string, ttl int64) bool
ChangeTTL changes TTL of the targetted room
func Exists(roomID string) bool
Exists return true if the given roomID's room still exists
func ExposeCommands()
ExposeCommands Exposes TCP or UDP/RUDP commands to execute room functions from the client
func GetAllRoomIDsOnNode() []string
GetAllRoomIDsOnNode returns all valid room IDs on the current node
func GetCreatedTime(roomID string) int64
GetCreatedTime returns the created time (in seconds) of the room
func GetMapping(roomID string) *mapping.Mapping
GetMapping returns a struct instance of mapping. room.UseMapping() must be called to use this
func GetMemberByID(roomID string, id string) *user.User
GetMemberByID returns member user by room ID and user ID
func GetMemberIDs(roomID string) []string
GetMemberIDs returns the list of member IDs (not SIDs)
func GetMemberIDsAndOwner(roomID string) ([]string, string, string)
GetMemberIDsAndOwner returns an array of all member IDs and owner ID and ownerSID
func GetMemberSIDs(roomID string) []string
GetMemberSIDs returns the list of member SIDs
func GetMemberSIDsAndOwner(roomID string) ([]string, string, string)
GetMemberSIDsAndOwner returns an array of all member IDs and owner ID and owner SID
func GetMemberUsers(roomID string) []*user.User
GetMemberUsers returns the list of member user struct instances
func GetMemberUsersAndOwnerUser(roomID string) ([]*user.User, *user.User)
GetMemberUsersAndOwnerUser returns an array of all member IDs and owner ID
func GetProperties(roomID string) map[string]interface{}
GetProperties returns properties of a room
[IMPORTANT] Room properties do NOT support structs. [IMPORTANT] when you handle properties, please use mesh.GetInt, mesh.GetInt8 etc as the value of properties maybe nil or float64 ------------------ Operation example: val := mesh.GetInt(properties, "counter") properties["counter"] = val + 1 // increment by 1 ----------------------------------- Use datacapsule.Capsule as property capsule := datacapsule.NewCapsule() capsule.Import(properties["memberNum"]) // getting property as a capsule (assumes properties["memberNum"] as map[string]interface) memberNum := capsule.GetAsInt8("memberNum")
func GetProperty(roomID string, name string) interface{}
GetProperty returns a property of the given room and property name. Returns nil if not found.
[IMPORTANT] Room properties do NOT support structs. [IMPORTANT] when you handle properties, please use mesh.GetInt, mesh.GetInt8 etc as the value of properties maybe nil or float64 ------------------ Operation example: val := mesh.GetInt(properties, "counter") properties["counter"] = val + 1 // increment by 1 ----------------------------------- Use datacapsule.Capsule as property capsule := datacapsule.NewCapsule() capsule.Import(properties["memberNum"]) // getting property as a capsule (assumes properties["memberNum"] as map[string]interface) memberNum := capsule.GetAsInt8("memberNum")
func GetRoomID(userData *user.User) string
GetRoomID returns roomID that the user has joined
func GetRoomNodeAddressList(roomID string) ([]string, error)
GetRoomNodeAddressList returns a list of internal node addresses that the room is stored
func GetRoomOwnerID(roomID string) string
GetRoomOwnerID returns the user ID of the owner of the room
func IncrProperty(roomID string, propName string, delta int64) bool
IncrProperty increments int64 property by given delta.
The property must be created before you execute this function.
If the property does not exist, it creates the property.
func IsUserInRoom(roomID string, userData *user.User) bool
IsUserInRoom returns true if the user is in the room
func Join( roomID string, userData *user.User, ver uint8, cmd uint16, message []byte, callback func(error, []string, string, int64, map[string]interface{}), )
Join Joins a room and notify the other members of the room on joining the room. If message is empty, Broadcast will not be sent. [IMPORTANT] The room to join may not be on the same server process and if that is the case, the client will move to where the room is [IMPORTANT] The properties map[string]interface{} that the callback function passes is NOT a copy and if you change this value you "may" change the actual property and corrupt the data. Please use this as read-only
Join(roomID, userData, ver, cmd, message, callback func(error, memberIDs, ownerID, createdTime, properties))
func Leave(roomID string, userData *user.User, ver uint8, cmd uint16, message []byte) error
Leave Leaves from a room and notify the other members on leaving. If message is empty Broadcast will not be sent.
func Message(roomID string, memberIDs []string, senderUser *user.User, ver uint8, cmd uint16, message []byte, reliable bool)
Message Sends a message to selected members of the room
func MigrateRoom(userData *user.User, room *Room, callback func(error, string, string))
MigrateRoom migrates a room to another node and moves its members to the new room This is inteded to used when the current node is about to shutdown, but you need to keep the current room. callback(error newRoomID string, oldRoomID string)
func NewRoom(userData *user.User, maxMembers int, allowEmpty bool, join bool, ttl int64, interval int64) (string, error)
NewRoom Creates a new room
userData - User data struct maxMembers - Maximum number of members allowed to join the room allowEmpty - If true, the room will not be deleted until TTL expires even if all members leave join - If true, the creator user will join the room automatically ttl - TTL of the room when it is empty 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 Reserve(roomID string, userData *user.User, memberIDs []string) error
Reserve fixes the member of users to join and rejects everyone else.
This operation is permitted only by the owner of the room.
The owner of the room will be included as a reversed member along with the list of member IDs.
func SetJoinCondition(cond func(string, *user.User) error)
SetJoinCondition registers a condition function to control room join. Returning an error will cause room join to fail
Join condition is NOT protected by mutext lock.
SetJoinCondition(condition func(roomID string, userData *user.User))
func SetOnAnnounce(roomID string, cb func(string, uint8, uint16, []byte))
SetOnAnnounce registeres a listener callback on Announce of room It captures Broadcast and Message as well
func SetOnDiscardCustomMessage(custom func(string, string) []byte)
SetOnDiscardCustomMessage sets a custom function to create a custom message on user discard to be sent to members
func SetOnMigrated(callback func(string, string))
SetOnMigrated registers a callback on migrated - The callbacks will be invoked on the server node where the room is migrated to
func SetOnRoomChange(cb func(string, []string, map[string]interface{}))
SetOnRoomChange registers a callback function on room change such as create, join, leave, property updates
func SetOnRoomDiscard(cb func(string))
SetOnRoomDiscard registers a callback function on room deletions
func SetOnRoomOwnerChange(cb func(interface{}))
SetOnRoomOwnerChange sets a callback for room owner change The paramter (interface{}) passed to the callback:
params["roomID"] = roomID string params["ownerID"] = ownerID string
func SetOnRoomPropertyUpdate(cb func(interface{}))
SetOnRoomPropertyUpdate sets a callback for room property updates. The paramter (interface{}) passed to the callback:
params["roomID"] = roomID string params["properties"] = properties interface{}
func SetOnTick(roomID string, callback func(string))
SetOnTick registers a callback on every heartbeat/echo tick on a room
func SetupAsTCPServer()
SetupAsTCPServer Sets up room package
func SetupAsUDPServer()
SetupAsUDPServer Sets up room package
func SetupAsWSServer()
SetupAsWSServer Sets up room package
func SetupBackup(backupNum int)
SetupBackup Enables room data backup to different nodes - maximum backup nodes you can set is 2
func SyncCreateTime(roomID string, ver uint8, cmd uint16, memberIDs []string)
SyncCreateTime sends room's created time (in seconds) to the selected (or all) members of the room
func ThrottledMessage(roomID string, memberIDs []string, senderUser *user.User, ver uint8, cmd uint16, throttleTime int64, message []byte, reliable bool)
ThrottledMessage sends a server push packet to the members given if the packet sent previously has past throttleTime previously set. If not, the packet will be "Dropped". throttleTime is in milliseconds.
func UpdateProperties(roomID string, operation func(properties map[string]interface{}) bool) map[string]interface{}
UpdateProperties updates or creates properties of a room and returns the updated or created properties.
[IMPORTANT] Room properties do NOT support structs.
[IMPORTANT] When you handle properties, please use mesh.GetInt, mesh.GetInt8 etc as the value of properties maybe nil or float64.
[IMPORTANT] operation function is protected by mutex lock, using another mutext lock within the function may cause a deadlock.
Operation example:
var updateErr error _ := room.UpdateProperties(roomID, func(properties map[string]interface{}) { capsule := datacapsule.NewCapsule() err := capsule.Import(properties["roomPropertyCapsule"].(map[string]interface{})) if err != nil { // this is how to propergate an error updateErr = err return } counter := capsule.GetAsInt("counter") counter++ capsule.SetAsInt("counter", counter) properties["roomPropertyCapsule"] = capsule.Export() }) if updateErr != nil { // handle update error here }
Use datacapsule.Capsule as property:
capsule := datacapsule.NewCapsule() capsule.SetAsInt8("memberNum", memberNum) properties["memberNum"] = capsule.Export() // setting capsule as a property
func UseMapping(roomID string, fieldSize int64)
UseMapping enables mapping (in-room field module)
Room Room data structure
type Room struct { ID string MaxMembers int AllowEmpty bool // an array of member SIDs Members []string // ID as key and SID as value MemberIDs map[string]string Properties map[string]interface{} // contains filtered or unexported fields }
func GetRoomByID(roomID string) *Room
GetRoomByID returns a room