Documentation ¶
Overview ¶
Package core contains all the tools to make and work with Users and Rooms.
A User is a client who has successfully logged into the server. You can think of clients who are not attached to a User as, for instance, someone in the login screen, but are still connected to the server. A client doesn't have to be a User to be able to call your CustomClientActions, so keep that in mind when making them (Refer to the Usage for CustomClientActions).
Users have their own variables which can be accessed and changed anytime. A User variable can be anything compatible with interface{}, so pretty much anything.
A Room represents a place on the server where a User can join other Users. Rooms can either be public or private. Private Rooms must be assigned an "owner", which is the name of a User, or the ServerName from ServerSettings. The server's name that will be used for ownership of private Rooms can be set with the ServerSettings option ServerName when starting the server. Though keep in mind, setting the ServerName in ServerSettings will prevent a User who wants to go by that name from logging in. Public Rooms will accept a join request from any User, and private Rooms will only accept a join request from someone who is on it's invite list. Only the owner of the Room or the server itself can invite Users to a private Room. But remember, just because a User owns a private room doesn't mean the server cannot also invite to the room via *Room.AddInvite() function.
Rooms have their own variables which can be accessed and changed anytime. Like User variables, a Room variable can be anything compatible with interface{}.
Index ¶
- Constants
- Variables
- func AutoLogIn(tag string, pass string, newPass string, dbID int, conn *websocket.Conn, ...) (string, helpers.GopherError)
- func GetRoomTypes() map[string]*RoomType
- func GetRoomsState() map[string]RoomRecoveryState
- func Login(userName string, dbID int, autologPass string, isGuest bool, remMe bool, ...) (string, helpers.GopherError)
- func Pause()
- func Resume()
- func RoomCount() int
- func SetChatMessageCallback(cb func(string, *Room, interface{}))
- func SetPrivateMessageCallback(cb func(*User, *User, interface{}))
- func SetServerMessageCallback(cb func(*Room, int, interface{}))
- func SetServerStarted(val bool)
- func SettingsSet(kickDups bool, name string, deleteOnLeave bool, sqlFeat bool, remMe bool, ...)
- func UserCount() int
- type Room
- func (r *Room) AddInvite(userName string) error
- func (r *Room) AddUser(user *User, connID string) error
- func (r *Room) ChatMessage(author string, message interface{}) error
- func (r *Room) DataMessage(message interface{}, recipients []string) error
- func (r *Room) Delete() error
- func (r *Room) GetUserMap() (map[string]*RoomUser, error)
- func (r *Room) GetVariable(key string) (interface{}, error)
- func (r *Room) GetVariables(keys []string) (map[string]interface{}, error)
- func (r *Room) InviteList() ([]string, error)
- func (r *Room) IsPrivate() bool
- func (r *Room) MaxUsers() int
- func (r *Room) Name() string
- func (r *Room) NumUsers() int
- func (r *Room) Owner() string
- func (r *Room) RemoveInvite(userName string) error
- func (r *Room) RemoveUser(user *User, connID string) error
- func (r *Room) ServerMessage(message interface{}, messageType int, recipients []string) error
- func (r *Room) SetVariable(key string, value interface{})
- func (r *Room) SetVariables(values map[string]interface{})
- func (r *Room) Type() string
- func (r *Room) VoiceStream(userName string, userSocket *websocket.Conn, stream interface{})
- type RoomRecoveryState
- type RoomType
- func (r *RoomType) BroadcastUserEnter() bool
- func (r *RoomType) BroadcastUserLeave() bool
- func (r *RoomType) CreateCallback() func(*Room)
- func (r *RoomType) DeleteCallback() func(*Room)
- func (r *RoomType) EnableBroadcastUserEnter() *RoomType
- func (r *RoomType) EnableBroadcastUserLeave() *RoomType
- func (r *RoomType) EnableVoiceChat() *RoomType
- func (r *RoomType) HasCreateCallback() bool
- func (r *RoomType) HasDeleteCallback() bool
- func (r *RoomType) HasUserEnterCallback() bool
- func (r *RoomType) HasUserLeaveCallback() bool
- func (r *RoomType) ServerOnly() bool
- func (r *RoomType) SetCreateCallback(callback func(*Room)) *RoomType
- func (r *RoomType) SetDeleteCallback(callback func(*Room)) *RoomType
- func (r *RoomType) SetUserEnterCallback(callback func(*Room, *RoomUser)) *RoomType
- func (r *RoomType) SetUserLeaveCallback(callback func(*Room, *RoomUser)) *RoomType
- func (r *RoomType) UserEnterCallback() func(*Room, *RoomUser)
- func (r *RoomType) UserLeaveCallback() func(*Room, *RoomUser)
- func (r *RoomType) VoiceChatEnabled() bool
- type RoomUser
- type User
- func (u *User) AcceptFriendRequest(friendName string) error
- func (u *User) ConnectionIDs() []string
- func (u *User) DataMessage(data interface{}, connID string)
- func (u *User) DatabaseID() int
- func (u *User) DeclineFriendRequest(friendName string) error
- func (u *User) FriendRequest(friendName string) error
- func (u *User) Friends() map[string]database.Friend
- func (u *User) GetVariable(key string, connID string) interface{}
- func (u *User) GetVariables(keys []string, connID string) map[string]interface{}
- func (u *User) Invite(invUser *User, connID string) error
- func (u *User) IsGuest() bool
- func (u *User) Join(r *Room, connID string) error
- func (u *User) Kick()
- func (u *User) Leave(connID string) error
- func (u *User) Logout(connID string)
- func (u *User) Name() string
- func (u *User) PrivateMessage(userName string, message interface{})
- func (u *User) RemoveFriend(friendName string) error
- func (u *User) RevokeInvite(revokeUser string, connID string) error
- func (u *User) RoomIn(connID string) *Room
- func (u *User) SetStatus(status int)
- func (u *User) SetVariable(key string, value interface{}, connID string)
- func (u *User) SetVariables(values map[string]interface{}, connID string)
- func (u *User) Socket(connID string) *websocket.Conn
- func (u *User) Status() int
Constants ¶
const ( MessageTypeChat = iota MessageTypeServer )
These represent the types of room messages the server sends.
const ( ServerMessageGame = iota ServerMessageNotice ServerMessageImportant )
These are the sub-types that a MessageTypeServer will come with. Ordered by their visible priority for your UI.
const ( StatusAvailable = iota // User is available StatusInGame // User is in a game StatusIdle // User is idle StatusOffline // User is offline )
These represent the four statuses a User could be.
Variables ¶
Functions ¶
func AutoLogIn ¶
func AutoLogIn(tag string, pass string, newPass string, dbID int, conn *websocket.Conn, connUser **User, clientMux *sync.Mutex) (string, helpers.GopherError)
AutoLogIn logs a user in automatically with RememberMe and SqlFeatures enabled in ServerSettings.
WARNING: This is only meant for internal Gopher Game Server mechanics. If you want the "Remember Me" (AKA auto login) feature, enable it in ServerSettings along with the SqlFeatures and corresponding options. You can read more about the "Remember Me" login in the project's usage section.
func GetRoomTypes ¶
GetRoomTypes gets a map of all the RoomTypes.
func GetRoomsState ¶
func GetRoomsState() map[string]RoomRecoveryState
GetRoomsState is only for internal Gopher Game Server mechanics.
func Login ¶
func Login(userName string, dbID int, autologPass string, isGuest bool, remMe bool, socket *websocket.Conn, connUser **User, clientMux *sync.Mutex) (string, helpers.GopherError)
Login logs a User in to the service.
func SetChatMessageCallback ¶
SetChatMessageCallback sets the callback function for when a *User sends a chat message to a *Room. The function passed must have the same parameter types as the following example:
func onChatMessage(userName string, room *core.Room, message interface{}) { //code... }
func SetPrivateMessageCallback ¶
SetPrivateMessageCallback sets the callback function for when a *User sends a private message to another *User. The function passed must have the same parameter types as the following example:
func onPrivateMessage(from *core.User, to *core.User, message interface{}) { //code... }
func SetServerMessageCallback ¶
SetServerMessageCallback sets the callback function for when the server sends a message to a *Room. The function passed must have the same parameter types as the following example:
func onServerMessage(room *core.Room, messageType int, message interface{}) { //code... }
The messageType value can be one of: core.ServerMessageGame, core.ServerMessageNotice, core.ServerMessageImportant, or a custom value you have set.
func SetServerStarted ¶
func SetServerStarted(val bool)
SetServerStarted is for Gopher Game Server internal mechanics only.
Types ¶
type Room ¶
type Room struct {
// contains filtered or unexported fields
}
Room represents a room on the server that Users can join and leave. Use core.NewRoom() to make a new Room.
WARNING: When you use a *Room object in your code, DO NOT dereference it. Instead, there are many methods for *Room for maniupulating and retrieving any information about them you could possibly need. Dereferencing them could cause data races in the Room fields that get locked by mutexes.
func GetRoom ¶
GetRoom finds a Room on the server. If the room does not exit, an error will be returned.
func NewRoom ¶
NewRoom adds a new room to the server. This can be called before or after starting the server. Parameters:
- name (string): Name of the Room
- rType (string): Room type name (Note: must be a valid RoomType's name)
- isPrivate (bool): Indicates if the room is private or not
- maxUsers (int): Maximum User capacity (Note: 0 means no limit)
- owner (string): The owner of the room. If provided a blank string, will set the owner to the ServerName from ServerSettings
func (*Room) AddInvite ¶
AddInvite adds a User to a private Room's invite list. This is only meant for internal Gopher Game Server mechanics. If you want a User to invite someone to a private room, use the *User.Invite() function instead.
NOTE: Remember that private rooms are designed to have an "owner", and only the owner should be able to send an invite and revoke an invitation for their Rooms. Also, *User.Invite() will send an invite notification message to the invited User that the client API can easily receive. Though if you wish to make your own implementations for sending and receiving these notifications, this function is safe to use.
func (*Room) AddUser ¶
AddUser adds a User to the Room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when adding a User to a Room with MultiConnect enabled. Otherwise, an empty string can be used.
func (*Room) ChatMessage ¶
ChatMessage sends a chat message to all Users in the Room.
func (*Room) DataMessage ¶
DataMessage sends a data message to the specified recipients in the Room. The parameter recipients can be nil or an empty slice of string. In which case, the data message will be sent to all Users in the Room.
func (*Room) Delete ¶
Delete deletes the Room from the server. Will also send a room leave message to all the Users in the Room that you can capture with the client APIs.
func (*Room) GetUserMap ¶
GetUserMap retrieves all the RoomUsers as a map[string]*RoomUser.
func (*Room) GetVariable ¶
GetVariable gets one of the Room's variables.
func (*Room) GetVariables ¶
GetVariables gets all the specified (or all if not) Room variables as a map[string]interface{}.
func (*Room) InviteList ¶
InviteList gets a private Room's invite list.
func (*Room) RemoveInvite ¶
RemoveInvite removes a User from a private Room's invite list. To make a User remove someone from their room themselves, use the *User.RevokeInvite() function.
NOTE: You can use this function safely, but remember that private rooms are designed to have an "owner", and only the owner should be able to send an invite and revoke an invitation for their Rooms. But if you find the need to break the rules here, by all means do so!
func (*Room) RemoveUser ¶
RemoveUser removes a User from the room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when removing a User from a Room with MultiConnect enabled. Otherwise, an empty string can be used.
func (*Room) ServerMessage ¶
ServerMessage sends a server message to the specified recipients in the Room. The parameter recipients can be nil or an empty slice of string. In which case, the server message will be sent to all Users in the Room.
func (*Room) SetVariable ¶
SetVariable sets a Room variable.
func (*Room) SetVariables ¶
SetVariables sets all the specified Room variables at once.
type RoomRecoveryState ¶
type RoomRecoveryState struct { T string // rType P bool // private O string // owner M int // maxUsers I []string // inviteList V map[string]interface{} // vars }
RoomRecoveryState is used internally for persisting room states on shutdown.
type RoomType ¶
type RoomType struct {
// contains filtered or unexported fields
}
RoomType represents a type of room a client or the server can make. You can only make and set options for a RoomType before starting the server. Doing so at any other time will have no effect at all.
func NewRoomType ¶
NewRoomType Adds a RoomType to the server. A RoomType is used in conjunction with it's corresponding callbacks and options. You cannot make a Room on the server until you have at least one RoomType to set it to. A RoomType requires at least a name and the serverOnly option, which when set to true will prevent the client API from being able to create, destroy, invite or revoke an invitation with that RoomType. Though you can always make a CustomClientAction to create a Room, initialize it, send requests, etc. When making a new RoomType you can chain the broadcasts and callbacks you want for it like so:
rooms.NewRoomType("lobby", true).EnableBroadcastUserEnter().EnableBroadcastUserLeave(). .SetCreateCallback(yourFunc).SetDeleteCallback(anotherFunc)
func (*RoomType) BroadcastUserEnter ¶
BroadcastUserEnter returns true if this RoomType has a user entry broadcast
func (*RoomType) BroadcastUserLeave ¶
BroadcastUserLeave returns true if this RoomType has a user leave broadcast
func (*RoomType) CreateCallback ¶
CreateCallback returns the function that this RoomType calls when a Room of this RoomType is created.
func (*RoomType) DeleteCallback ¶
DeleteCallback returns the function that this RoomType calls when a Room of this RoomType is deleted.
func (*RoomType) EnableBroadcastUserEnter ¶
EnableBroadcastUserEnter sends an "entry" message to all Users in the Room when another User enters the Room. You can capture these messages on the client side easily with the client APIs.
Note: You must call this BEFORE starting the server in order for it to take effect.
func (*RoomType) EnableBroadcastUserLeave ¶
EnableBroadcastUserLeave sends a "left" message to all Users in the Room when another User leaves the Room. You can capture these messages on the client side easily with the client APIs.
Note: You must call this BEFORE starting the server in order for it to take effect.
func (*RoomType) EnableVoiceChat ¶
EnableVoiceChat Enables voice chat for this RoomType.
Note: You must call this BEFORE starting the server in order for it to take effect.
func (*RoomType) HasCreateCallback ¶
HasCreateCallback returns true if this RoomType has a creation callback.
func (*RoomType) HasDeleteCallback ¶
HasDeleteCallback returns true if this RoomType has a delete callback.
func (*RoomType) HasUserEnterCallback ¶
HasUserEnterCallback returns true if this RoomType has a user enter callback.
func (*RoomType) HasUserLeaveCallback ¶
HasUserLeaveCallback returns true if this RoomType has a user leave callback.
func (*RoomType) ServerOnly ¶
ServerOnly returns true if the RoomType can only be manipulated by the server.
func (*RoomType) SetCreateCallback ¶
SetCreateCallback is executed when someone creates a Room of this RoomType by setting the creation callback. Your function must take in a Room object as the parameter which is a reference of the created room.
Note: You must call this BEFORE starting the server in order for it to take effect.
func (*RoomType) SetDeleteCallback ¶
SetDeleteCallback is executed when someone deletes a Room of this RoomType by setting the delete callback. Your function must take in a Room object as the parameter which is a reference of the deleted room.
Note: You must call this BEFORE starting the server in order for it to take effect.
func (*RoomType) SetUserEnterCallback ¶
SetUserEnterCallback is executed when a User enters a Room of this RoomType by setting the User enter callback. Your function must take in a Room and a string as the parameters. The Room is the Room in which the User entered, and the string is the name of the User that entered.
Note: You must call this BEFORE starting the server in order for it to take effect.
func (*RoomType) SetUserLeaveCallback ¶
SetUserLeaveCallback is executed when a User leaves a Room of this RoomType by setting the User leave callback. Your function must take in a Room and a string as the parameters. The Room is the Room in which the User left, and the string is the name of the User that left.
Note: You must call this BEFORE starting the server in order for it to take effect.
func (*RoomType) UserEnterCallback ¶
UserEnterCallback returns the function that this RoomType calls when a User enters a Room of this RoomType.
func (*RoomType) UserLeaveCallback ¶
UserLeaveCallback returns the function that this RoomType calls when a User leaves a Room of this RoomType.
func (*RoomType) VoiceChatEnabled ¶
VoiceChatEnabled returns true if voice chat is enabled for this RoomType
type RoomUser ¶
type RoomUser struct {
// contains filtered or unexported fields
}
RoomUser represents a User inside of a Room. Use the *RoomUser.User() function to get a *User from a *RoomUser
func (*RoomUser) ConnectionIDs ¶
ConnectionIDs returns a []string of all the RoomUser's connection IDs. With MultiConnect in ServerSettings enabled, this will give you all the connections for this User that are currently in the Room. Otherwise, if you want all the User's connection IDs (not just the connections in the specified Room), use *User.ConnectionIDs() after getting the *User object with the *RoomUser.User() function.
type User ¶
type User struct {
// contains filtered or unexported fields
}
User represents a client who has logged into the service. A User can be a guest, join/leave/create rooms, and call any client action, including your custom client actions. If you are not using the built-in authentication, be aware that you will need to make sure any client who has not been authenticated by the server can't simply log themselves in through the client API. A User has a lot of useful information, so it's highly recommended you look through all the *User methods to get a good understanding about everything you can do with them.
WARNING: When you use a *User object in your code, DO NOT dereference it. Instead, there are many methods for *User for retrieving any information about them you could possibly need. Dereferencing them could cause data races (which will panic and stop the server) in the User fields that get locked for synchronizing access.
func GetUser ¶
GetUser finds a logged in User by their name. Returns an error if the User is not online.
func (*User) AcceptFriendRequest ¶
AcceptFriendRequest accepts a friend request from another User by their name.
func (*User) ConnectionIDs ¶
ConnectionIDs returns a []string of all the User's connection IDs
func (*User) DataMessage ¶
DataMessage sends a data message directly to the User.
func (*User) DatabaseID ¶
DatabaseID gets the database table index of the User.
func (*User) DeclineFriendRequest ¶
DeclineFriendRequest declines a friend request from another User by their name.
func (*User) FriendRequest ¶
FriendRequest sends a friend request to another User by their name.
func (*User) Friends ¶
Friends gets the Friend list of the User as a map[string]database.Friend where the key string is the friend's User name.
func (*User) GetVariable ¶
GetVariable gets one of the User's variables by it's key. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when getting a User's variables with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) GetVariables ¶
GetVariables gets the specified (or all if nil) User variables as a map[string]interface{}. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when getting a User's variables with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) Invite ¶
Invite allows Users to invite other Users to their private Rooms. The inviting User must be in the Room, and the Room must be private and owned by the inviting User. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when making a User invite another with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) Join ¶
Join makes a User join a Room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when making a User join a Room with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) Leave ¶
Leave makes a User leave their current room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when making a User leave a Room with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) Logout ¶
Logout logs a User out from the service. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when logging a User out with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) PrivateMessage ¶
PrivateMessage sends a private message to another User by name.
func (*User) RemoveFriend ¶
RemoveFriend removes a friend from this this User and this User from the friend's Friend list.
func (*User) RevokeInvite ¶
RevokeInvite revokes the invite to the specified user to their current Room, provided they are online, the Room is private, and this User is the owner of the Room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when making a User revoke an invite with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) RoomIn ¶
RoomIn gets the Room that the User is currently in. A nil Room pointer means the User is not in a Room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when getting a User's Room with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) SetStatus ¶
SetStatus sets the status of a User. Also sends a notification to all the User's Friends (with the request status "accepted") that they changed their status.
func (*User) SetVariable ¶
SetVariable sets a User variable. The client API of the User will also receive these changes. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when setting a User's variables with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) SetVariables ¶
SetVariables sets all the specified User variables at once. The client API of the User will also receive these changes. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when setting a User's variables with MultiConnect enabled. Otherwise, an empty string can be used.
func (*User) Socket ¶
Socket gets the WebSocket connection of a User. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when getting a User's socket connection with MultiConnect enabled. Otherwise, an empty string can be used.