Android API provides you with ‘gamooga.jar’ containing one class - GamoogaClient and four interfaces - ConnectCallback , MessageCallback , DisconnectCallback , ErrorCallback , all in com.gamooga.client namespace. ‘gamooga.jar’ is available in the SDK.
The Android API is very similar to other client APIs except that the callbacks are implemented as instances of anonymous classes of the above interfaces (rather than plain function callbacks).
You require to use the GamoogaClient class to connect and communicate with Gamooga servers (or the local development server). Instantiate the class like below.
Usage:
import com.gamooga.client.GamoogaClient;
...
GamoogaClient gc = new GamoogaClient(devServer);
// or
GamoogaClient gc = new GamoogaClient();
| argument | type |
|---|---|
| devServer | String (optional) |
devServer is the ip/domain address of the dev server (provided in the SDK). To connect to production, omit the devServer argument from the constructor.
Example:
GamoogaClient gc = new GamoogaClient("127.0.0.1"); //to connect to development server
GamoogaClient gc = new GamoogaClient(); //to connect to production
Usage:
gc.connectToRoom(app_id, app_uuid)
| argument | type |
|---|---|
| app_id | int |
| app_uuid | String |
This method is used to connect to the room of your gamlet with app_id and app_uuid as the application id and the application uuid.
NOTE: Since the dev server in the SDK can only run a single gamlet, you can provide any app_id and app_uuid while connecting to dev server.
Example:
gc.connectToRoom(12, "f824cdbc-46b1-11e1-9008-00266c101cae");
Usage:
gc.createConnectToSession(app_id, app_uuid);
| argument | type |
|---|---|
| app_id | int |
| app_uuid | String |
This method is used to create a new session of the gamlet with app_id and app_uuid as the application id and the application uuid and connect to this newly created session.
NOTE: Since the dev server in the SDK can only run a single gamlet, you can provide any app_id and app_uuid while connecting to dev server.
Example:
gc.createConnectToSession(12, "f824cdbc-46b1-11e1-9008-00266c101cae");
Usage:
gc.connectToSession(sess_id, app_uuid);
| argument | type |
|---|---|
| sess_id | int |
| app_uuid | String |
This method is used to connect to the session sess_id of the gamlet with app_uuid as the application uuid.
NOTE: Since the dev server in the SDK can only run a single gamlet, you can provide any app_uuid while connecting to dev server.
Example:
gc.connectToSession(12345, "f824cdbc-46b1-11e1-9008-00266c101cae");
Usage:
gc.onconnect(onconn_cb);
| argument | type |
|---|---|
| onconn_cb | ConnectCallback |
This method is used to attach a class object onconn_cb implementing ConnectCallback interface as a callback to the onconnect event when the client connects to the server. handle method of onconn_cb is executed as soon as the connection to server succeeds. (Its easier to use anonymous classes instead of defining a new class implementing the ConnectCallback interface, hence we, from now on, use anonymous classes in all other methods requiring callbacks).
Example:
gc.onconnect(new ConnectCallback() {
@Override
public void handle() {
Log.d("myapp", "connected to server");
}
});
Usage:
gc.onmessage(msg_type, onmsg_cb);
| argument | type |
|---|---|
| msg_type | String |
| onmsg_cb | MessageCallback |
This method is used to attach an anonymous class object onmsg_cb implementing MessageCallback as a callback to the onmessage event when a server sends a message. Server can send messages of different types. This method can be used to attach a callback to a type of message. handle method of onmsg_cb is called as soon as a message of type msg_type is received from the server. handle itself is passed one argument: msg (data type: Object) which is the message sent from the server.
Example:
gc.onmessage("userjoin", new MessageCallback() {
@Override
public void handle(Object msg) {
Log.d("myapp", "received message type: userjoin, with message: "+(String)msg);
}
});
Usage:
gc.ondisconnect(ondisconn_cb);
| argument | type |
|---|---|
| ondisconn_cb | DisconnectCallback |
This method is used to attach an anonymous class object ondisconn_cb implementing DisconnectCallback as a callback to the ondisconnect event when the client disconnects from the server. handle method of ondisconn_cb is called as soon as the client disconnects.
Example:
gc.ondisconnect(new DisconnectCallback() {
@Override
public void handle() {
Log.d("myapp", "disconnected from server");
}
});
Usage:
gc.onerror(onerror_cb);
| argument | type |
|---|---|
| onerror_cb | ErrorCallback |
This method is used to attach an anonymous class object onerror_cb implementing ErrorCallback as a callback to the onerror event when an error occurs at client or server side. handle method of onerror_cb is called as soon as the error occurs. handle itself is passed one argument: errno (data type: int) is the error number denoting the error that occured.
| errno | GamoogaClient constant | reason |
|---|---|---|
| 3 | GamoogaClient.CLIENT_SERVER_BUSY | Server has too many messages pending to process for this gamlet and hence rejected a message that is sent (we currently queue upto 100 pending messages to be processed by server before we report this error) |
| 4 | GamoogaClient.CLIENT_SERVER_ERROR | Server is in a state of error, due to error in its gamlet code |
| 6 | GamoogaClient.CLIENT_IN_DATA_EXCEED | Client sent too much data (> 8KB) to the server in a single send |
| 7 | GamoogaClient.CLIENT_OUT_DATA_EXCEED | Server sent too much data (> 8KB) to the client in a single send (*) |
| 101 | GamoogaClient.IO_ERROR | Could not connect to server, probably because server is down |
| 201 | GamoogaClient.WRONG_APP_ID | Could not connect to room, wrong APP_ID |
| 202 | GamoogaClient.WRONG_APP_UUID | Could not connect to room/session, wrong APP_UUID |
| 203 | GamoogaClient.APP_ID_AND_UUID_NOT_PROVIDED | APP_ID/APP_UUID not provided in connect* APIs |
| 204 | GamoogaClient.LIMITS_REACHED | Gamlet usage has reached the currently subscribed plan limits |
| 205 | GamoogaClient.GAMLET_UNDEPLOYED | Gamlet is currently undeployed |
| 301 | GamoogaClient.API_ERROR | Other error while connecting to server |
(*) - This error is best reported on the server side, but for effeciency of the server, this error is reported on the client side only.
Example:
gc.onerror(new ErrorCallback() {
@Override
public void handle(int errno) {
Log.d("myapp", "caught error: "+errno);
}
});
Usage:
gc.send(msg_type, msg);
| argument | type |
|---|---|
| msg_type | String |
| msg | Object |
This function is used to send a message msg of type msg_type to the room or session.
Example:
gc.send("userchat", "hi there");
Usage:
int sess_id = gc.getSessId();
This method returns the id of the session the client is connected to. If its connected to room or not yet connected to session, then this method returns 0.
Usage:
gc.disableLogMsg();
When connected to development server, GamoogaClient logs all events using Log.d. You can use this message to disable that logging. (When connected to production, logging is disabled by default, you can enable it using the next method.)
Usage:
gc.enableLogMsg();
You can use this function to enable logging events using Log.d.