Accessing the data object in ExtensionResponse event

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Lapo, Bax

Post Reply
jeff_o
Posts: 13
Joined: 03 Oct 2008, 20:59

Accessing the data object in ExtensionResponse event

Post by jeff_o »

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
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

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:

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
jeff_o
Posts: 13
Joined: 03 Oct 2008, 20:59

Post by jeff_o »

Awesome that works, thank you!
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

Perfect! Good luck
jeff_o
Posts: 13
Joined: 03 Oct 2008, 20:59

Post by jeff_o »

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.
Post Reply