Hello, I am trying to access the data object, as done in the API Documentation example:
SFSEvent.onExtensionResponse += OnExtensionResponse;
public void OnExtensionResponse(object data, string type)
{
string command = data._cmd;
....
}
However, this throws an error:
Error 1 'object' does not contain a definition for '_cmd' and no extension method '_cmd' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Any help would be much appreciated!
Edit: Btw, I am using SF C# beta 2
Accessing the data object in ExtensionResponse event
-
- Posts: 1297
- Joined: 14 Mar 2008, 07:52
- Location: Sweden
Hi jeff_o
Its a problem in the example provided in the API documentation. I fixed that after beta 2.
Here is a working example from the tic-tac-toe tutorial I am working on:
Unfortunately the C# API is slightly different than what is possible in ActionScript, so depending on how the data was sent from the server (XML, JSon or raw string) the data object will be of a different type. For XML the API converts it into a SFSObject like above. For Json you will get a JsonData object (check out the LitJSon manual for examples http://litjson.sourceforge.net/doc/manual.html) and for string you will get it as a string if I recall.
Beware that the API is extremely fickle about using correct types - if you try to pull a double out of a GetString then it will go bananas.
/Thomas
Its a problem in the example provided in the API documentation. I fixed that after beta 2.
Here is a working example from the tic-tac-toe tutorial I am working on:
Code: Select all
public void OnExtensionResponse(object data, string type) {
// We only use XML based messages in this tutorial, so ignore string and json types
if ( type == SmartFoxClient.XTMSG_TYPE_XML ) {
// For XML based communication the data object is a SFSObject
SFSObject dataObject = (SFSObject)data;
switch ( dataObject.GetString("_cmd") ) {
case "start":
StartGame((int)dataObject.GetNumber("t"),
(int)dataObject.GetNumber("p1i"),
(int)dataObject.GetNumber("p2i"),
dataObject.GetString("p1n"),
dataObject.GetString("p2n")
);
break;
case "stop":
UserLeft();
break;
case "move":
MoveReceived((int)dataObject.GetNumber("t"), (int)dataObject.GetNumber("x"), (int)dataObject.GetNumber("y"));
break;
case "specStatus":
SetSpectatorBoard((int)dataObject.GetNumber("t"),
(int)dataObject.GetNumber("p1i"),
(int)dataObject.GetNumber("p2i"),
dataObject.GetString("p1n"),
dataObject.GetString("p2n"),
dataObject.GetBool("status"),
dataObject.Get("board"));
break;
case "win":
case "tie":
ShowWinner(dataObject.GetString("_cmd"), (int)dataObject.GetNumber("w"));
break;
}
}
}
Unfortunately the C# API is slightly different than what is possible in ActionScript, so depending on how the data was sent from the server (XML, JSon or raw string) the data object will be of a different type. For XML the API converts it into a SFSObject like above. For Json you will get a JsonData object (check out the LitJSon manual for examples http://litjson.sourceforge.net/doc/manual.html) and for string you will get it as a string if I recall.
Beware that the API is extremely fickle about using correct types - if you try to pull a double out of a GetString then it will go bananas.
/Thomas
-
- Posts: 1297
- Joined: 14 Mar 2008, 07:52
- Location: Sweden
I have one more related question...is it possible to access an actionscript array sent back in the data object of the extensionresponse? I saw that in your example you did a dataObject.Get("board"). This looks like it might be what I am trying to do, however when I attempted to do this I got an SFSObject and was not able to cast it to a String[] array (perhaps I should be casting to a different data type?) To put this in context, I am trying to get a list of all users on the server, and on the Smartfox server side I have the list of users in an actionscript array which I then send back to the C# client.
Return to “.Net / Unity3D API”
Who is online
Users browsing this forum: No registered users and 18 guests