I just started working on Unity3D and SmartFox. Starting with the Multiplayer Island demo available here, I modified one of the extensions to allow the game access to a H2 database.
Everything seems to work fine with the connection, client connects the server, server performs the sql query and send back to client the results of the query. When sending simple variables like a string or a number, I have no problem in recovering the data at the client side. However, and here is my question, I have no idea how to recover a whole array of data from the database.
To clarify it a little bit more, I'll post the code of the communications.
From the client side connecting to the server:
Code: Select all
Hashtable h = new Hashtable();
smartFoxClient.SendXtMessage("json", "getList", h, SmartFoxClient.XTMSG_TYPE_XML);
Debug.Log("Enviando");
Handling the server connection and performing the database query:
Code: Select all
function handleRequest(cmd, params, user, fromRoom)
{
if (cmd == "getList")
{
sendComputerList(user)
}
}
function sendComputerList(user)
{
var sql = "SELECT * FROM SAVEHTEST"
// execute query on DB
// queryRes is a ResultSet object
var queryRes = dbase.executeQuery(sql)
if (queryRes != null)
{
trace("Entering the query")
var response = {}
response._cmd = "getList"
/*
* Here's the new SmartFoxServer 1.6 framework update:
* You can pass the query ResultSet DIRECTLY to the send response
* which will serialize the record set behind the scenes!
*/
response.db = []
// Cycle through all records in the ResultSet
for (var i = 0; i < queryRes.size(); i++)
{
// Get a record
var tempRow = queryRes.get(i)
// This object will hold the record data that we'll send to the client
var item = {}
// From the record object we can get each field value
item.id = tempRow.getItem("ID")
item.name = tempRow.getItem("NAME")
response.db.push( item )
}
_server.sendResponse(response, -1, null, [user])
}
else
trace("Error in the query")
}
Reading the data again at the client side:
Code: Select all
public void OnExtensionResponse(object data, string type)
{
SFSObject dataObject = (SFSObject)data;
double IDItem = 0;
string nameItem = "";
Hashtable results = (Hashtable)dataObject.Get("db");
for (int i = 0; i < 2; i++)
{
Hashtable item = (Hashtable)results.Get(i);
IDItem = (double)item[0];
nameItem = item.GetString("name");
Debug.Log("ENTRADA " + i + " : ID " + IDItem);
}
}
With this, I get problems at the line where I create the variable "results", it just won't work, it freezes and don't execute any other instruction in the function. The Hastable was the last type I tried for reading the data. I've tried with SFSObject, ArrayList, Arrays, and I don't know what else.
Maybe I'm doing something terribly wrong outside the function and I just can't figure it out, but my guess stays in that line. I've just been working a couple of weeks in Unity and SmartFox, hence my question in this forum: How can I read that "db" variable from the server side in the client side?
If more information is required, I'll post it. Thanks in advance!
José Franco