Error on Unity WebGL processing sfsObject.GetUtfStringArray

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Tatanan
Posts: 112
Joined: 07 Jan 2014, 12:12
Contact:

Error on Unity WebGL processing sfsObject.GetUtfStringArray

Postby Tatanan » 15 Sep 2015, 14:35

Hi, I have find and strange bug on Unity Client Api for WebGL (Unity 5.2.0f3 and pre-release SfsApi you sent by email related with this issue viewtopic.php?f=20&t=17994).

This issue only occurs when I switch Unity Editor to WebGL ,on other platforms it works fine ( Ios, Android, Standalone, WebPlayer ).

This issue is that:

The extension sends and UtfStringArray where each string is Json serialized data. But when clients api receive the SfsObject and tries to get the data this error is logged:

InvalidCastException: Cannot cast from source type to destination type.
Sfs2X.Entities.Data.SFSArray.GetValue[String] (Int32 index)
Sfs2X.Entities.Data.SFSArray.GetUtfString (Int32 index)
Sfs2X.Entities.Data.SFSObjectLite.GetUtfStringArray (System.String key)


In order to find out what was happening I realise that instead of receive an UtfStringArray I receive a SfsArray that each element is a SfsObject with the data of the json the server sends. So is like SfsClient Api detects that the strings are Json data and deserialize them.

Here is the code on Extension Side:

Code: Select all

List<String> playersInfo;
playersInfo.add("{/"name/": /"Player A/" ,/"score/": 100}");
playersInfo.add("{/"name/": /"Player B/" ,/"score/": 150}");

ISFSObject response = SFSObject.newInstance();
response.putUtfStringArray("dataAlias", playersInfo);
send("playerInfoRequest" response, sender);


Here is the code on client side (WEBGL):

Code: Select all

ISFSObject sfsResponse = (ISFSObject) evt.Params [ExtensionParamsTypes.PARAMS];
Debug.Log(sfsResponse.GetSFSArray("dataAlias").GetSFSObject(0).GetUtfString("name")) // Player A
Debug.Log(sfsResponse.GetSFSArray("dataAlias").GetSFSObject(0).GetInt("score")) // 100
Debug.Log(sfsResponse.GetSFSArray("dataAlias").GetSFSObject(1).GetUtfString("name")) // Player B
Debug.Log(sfsResponse.GetSFSArray("dataAlias").GetSFSObject(1).GetInt("score")) // 150
Debug.Log(sfsResponse.GetUtfStringArray("dataAlias")) //  ERROR - Throws message above


And Here is the code on client side (Other Platforms):

Code: Select all

ISFSObject sfsResponse = (ISFSObject) evt.Params [ExtensionParamsTypes.PARAMS];
Debug.Log(sfsResponse.GetUtfStringArray("dataAlias")[0]) // {"name": "Player A" , "score": 100}
Debug.Log(sfsResponse.GetUtfStringArray("dataAlias")[1]) // {"name": "Player B" , "score": 150}
Debug.Log(sfsResponse.GetSFSArray("dataAlias")) // ERROR


Thanks
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Error on Unity WebGL processing sfsObject.GetUtfStringAr

Postby Lapo » 16 Sep 2015, 10:35

WebGL uses websocket which in turn uses a text-based protocol encoded in JSON.
I would not recommend working with JSON data, especially with WebGL, because you're nesting JSON within JSON.

Instead I would simply create an object with the specific types:

Code: Select all

SFSObject item = new SFSObject()
item.putUtfString("name", "Player A");
item.putInt("score", 100);

Instead of this:

Code: Select all

playersInfo.add("{/"name/": /"Player A/" ,/"score/": 100}");


If you want to save even more bytes you can use an SFSArray instead of the SFSObject, so you get rid of the keys and just use numeric positions. Knowing that at pos=0 you have the name, pos=1 the score etc...

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
andersemil
Posts: 10
Joined: 19 Nov 2015, 14:11
Location: Copenhagen, Denmark

Re: Error on Unity WebGL processing sfsObject.GetUtfStringAr

Postby andersemil » 19 Nov 2015, 14:31

This problem is not limited to JSON strings.

I always get an invalidcastexception on utfstringarray, even with simple text strings.
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Error on Unity WebGL processing sfsObject.GetUtfStringAr

Postby Lapo » 19 Nov 2015, 14:39

Hi,
you report isn't clear enough. Can you please provide a specific example that causes the problem you've mentioned?

Also knowing which Server and client API version you're using wouldn't hurt.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
andersemil
Posts: 10
Joined: 19 Nov 2015, 14:11
Location: Copenhagen, Denmark

Re: Error on Unity WebGL processing sfsObject.GetUtfStringAr

Postby andersemil » 20 Nov 2015, 13:04

Using SFS2X_API_CSharp_v1.6.2 and SFS2X community edition v2.9.

I have two types of clients, a WebGL and a mobile build which send Object Messages containing an array of strings. Code looks like this:

Code: Select all

   var dataObj = new SFSObject();
        dataObj.PutUtfStringArray("qa", aa);
        sfs.Send(new ObjectMessageRequest(dataObj, GameRoom));


When the mobile build receives and object message from a webgl build, the dataObj member "qa" is NOT of type UtfStringArray, but rather a SFSArray containing strings. So I have to handle it differently depending on the build. Currently I use this rather ugly workaround:

Code: Select all

            string[] aa;
            try {
               aa = dataObj.GetUtfStringArray("qa");
            } catch (System.InvalidCastException ex) {
               var aarr = dataObj.GetSFSArray("qa");
               aa = new string[aarr.Count];
               int i=0;
               foreach (var a in aarr) {
                  aa[i++] = a as string;
               }
            }
User avatar
Bax
Site Admin
Posts: 4610
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Error on Unity WebGL processing sfsObject.GetUtfStringAr

Postby Bax » 21 Nov 2015, 11:22

I did a simple test: the Unity client sends a request to the server and the Extension returns an object containing a string array.
Everything works fine unless you send JSON inside the array. As Lapo already mentioned in this post, this is not supported.
If you send simple strings, everything is fine.
Paolo Bax
The SmartFoxServer Team

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 28 guests