Documentation ¶
Overview ¶
Package actions contains all the tools for making your custom client actions.
Index ¶
- Constants
- func HandleCustomClientAction(action string, data interface{}, user *core.User, conn *websocket.Conn, ...)
- func New(actionType string, dataType int, callback func(interface{}, *Client)) error
- func Pause()
- func Resume()
- func SetServerStarted(val bool)
- type Client
- type ClientError
- type CustomClientAction
Constants ¶
const ( ErrorMismatchedTypes = iota + 1001 // Client didn't pass the right data type for the given action ErrorUnrecognizedAction // The custom action has not been defined )
Default `ClientError`s
const ( DataTypeBool = iota // Boolean data type DataTypeInt // int, int32, and int64 data types DataTypeFloat // float32 and float64 data types DataTypeString // string data type DataTypeArray // []interface{} data type DataTypeMap // map[string]interface{} data type DataTypeNil // nil data type )
These are the accepted data types that a client can send with a CustomClientMessage. You must use one of these when making a new CustomClientAction, or it will not work. If a client tries to send a type of data that doesnt match the type specified for that action, the CustomClientAction will send an error back to the client and skip executing your callback function.
Variables ¶
This section is empty.
Functions ¶
func HandleCustomClientAction ¶
func HandleCustomClientAction(action string, data interface{}, user *core.User, conn *websocket.Conn, connID string)
HandleCustomClientAction handles your custom client actions.
WARNING: This is only meant for internal Gopher Game Server mechanics. Your CustomClientAction callbacks are called from this function. This could spawn errors and/or memory leaks.
func New ¶
New creates a new `CustomClientAction` with the corresponding parameters:
- actionType (string): The type of action
- (*)callback (func(interface{},Client)): The function that will be executed when a client calls this `actionType`
- dataType (int): The type of data this action accepts. Options are `DataTypeBool`, `DataTypeInt`, `DataTypeFloat`, `DataTypeString`, `DataTypeArray`, `DataTypeMap`, and `DataTypeNil`
(*)Callback function format:
func yourFunction(actionData interface{}, client *Client) { //... // optional client response client.Respond("example", nil); }
- actionData: The data the client sent along with the action
- client: A `Client` object representing the client that sent the action
Note: This function can only be called BEFORE starting the server.
func SetServerStarted ¶
func SetServerStarted(val bool)
SetServerStarted is for Gopher Game Server internal mechanics only.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client objects are created and sent along with your CustomClientAction callback function when a client sends an action.
func (*Client) ConnectionID ¶
ConnectionID gets the connection ID of the Client. This is only used if you have MultiConnect enabled in ServerSettings and you need to, for instance, call *User functions with the Client's *User obtained with the client.User() function. If you do, use client.ConnectionID() when calling any *User functions from a Client.
func (*Client) Respond ¶
func (c *Client) Respond(response interface{}, err ClientError)
Respond sends a CustomClientAction response to the client. If an error is provided, only the error mesage will be received by the Client (the response parameter will not be sent as well). It's perfectly fine to not send back any response if none is needed.
NOTE: A response can only be sent once to a Client. Any more calls to Respond() on the same Client will not send a response, nor do anything at all. If you want to send a stream of messages to the Client, first get their User object with *Client.User(), then you can send data messages directly to the User with the *User.DataMessage() function.
type ClientError ¶
type ClientError struct {
// contains filtered or unexported fields
}
ClientError is used when an error is thrown in your CustomClientAction. Use `actions.NewError()` to make a ClientError object. When no error needs to be thrown, use `actions.NoError()` instead.
func NewError ¶
func NewError(message string, id int) ClientError
NewError creates a new error with a provided message and ID.
func NoError ¶
func NoError() ClientError
NoError is used when no error needs to be thrown in your `CustomClientAction`.
type CustomClientAction ¶
type CustomClientAction struct {
// contains filtered or unexported fields
}
CustomClientAction is an action that you can handle on the server from a connected client. For instance, a client can send to the server a CustomClientAction type "setPosition" that comes with parameters as an object {x: 2, y: 3}. You just need to make a callback function for the CustomClientAction type "setPosition", and as soon as the action is received by the server, the callback function will be executed concurrently in a Goroutine.