Unable to send complex datatype from JS using PersistentConnection #327

Closed
taschmidt opened this Issue Apr 20, 2012 · 7 comments

Comments

Projects
None yet
5 participants

If I make the following call from JS:

 $.connection.send({"one":1, "two":2})

The resulting call to OnReceivedAsync comes in with data set to null. Sniffing the traffic, the SignalR JS serializes the request with two form variables: data[one] and data[two]. But on ForeverTransport.cs: line 150, it's currently doing:

 string data = Context.Request.Form["data"];

So it never picks up and reconstitutes my object. That seems like an oversight.

tim

Owner

davidfowl commented Apr 20, 2012

First thing, That looks like the wrong syntax. $.connection is't an instance of your connection so you can't call send from it.
Second thing, persistent connection is strings in strings out, you need to send data serialized. (check out the samples).
Third thing, take a look at the Raw sample, it sends complex objects:

https://github.com/SignalR/SignalR/blob/master/samples/SignalR.Hosting.AspNet.Samples/Raw/index.htm
https://github.com/SignalR/SignalR/blob/master/samples/SignalR.Hosting.AspNet.Samples/Raw/Raw.cs

davidfowl closed this Apr 20, 2012

Oops, wrong syntax, yes. I meant basically:

 conn.send({one:1, two:2});

But it definitely isn't working in v0.4 for the reason I said.

Owner

davidfowl commented Apr 20, 2012

That's because you're using it incorrectly. The behavior you see is by design. Look at the sample and the second point I stated above.

Ah, sorry, didn't read all of your post. One suggestion, couldn't you check if it's an object and call 'stringify' in the SignalR js? It's a little misleading from an implementation point of view. In most things, you try to be consistent between client and server semantics but on the server side, I can call Send(obj) with a complex object, so I naturally tried to do the same thing on the client side.

Owner

davidfowl commented Apr 20, 2012

True. It's not symmetric, we could improve that on the client side.

Moving to the backlog for later consideration.

davidfowl reopened this Apr 20, 2012

Owner

davidfowl commented Jun 27, 2013

@NTaylorMullen let's do this for 2.0

DamianEdwards was assigned Jul 2, 2013

@DamianEdwards DamianEdwards added a commit that referenced this issue Jul 3, 2013

@DamianEdwards DamianEdwards Auto JSON stringify objects for connections in JS client
- Updated websockets & ajax send to encode message using connection.json if the message is anything other than a string
- Updated call sites in SignalR and samples that were manually JSON encoding to just pass the objects themselves
- #327
1f36e24

@DamianEdwards DamianEdwards added a commit that referenced this issue Jul 3, 2013

@DamianEdwards DamianEdwards Auto JSON stringify objects for connections in JS client
- Updated websockets & ajax send to encode message using connection.json if the message is anything other than a string
- Updated call sites in SignalR and samples that were manually JSON encoding to just pass the objects themselves
- #327
b567a0c

@DamianEdwards DamianEdwards added a commit that referenced this issue Jul 4, 2013

@DamianEdwards DamianEdwards Addressed some review feedback
- Used typeof instead of $.type
- Explicitly handle undefined & null
- Added unit tests for all cases
- Updated functional test to cover undefined & null case
- #327
30fc614
Contributor

gustavo-armenta commented Jul 10, 2013

ran automated test in all transports
verified some additional values to stringify

        console.log("beginTest");
        console.log(hubConnection.json.stringify(null));
        console.log(hubConnection.json.stringify(undefined));
        console.log(hubConnection.json.stringify("text"));
        console.log(hubConnection.json.stringify({ "field": "text" }));
        console.log(hubConnection.json.stringify({ "field": null }));
        console.log(hubConnection.json.stringify({ "field": undefined }));
        console.log(window.JSON.stringify({ "field": undefined }));
        console.log(hubConnection.json.stringify({ "field": [] }));
        console.log(hubConnection.json.stringify({ "field": ["a"] }));
        console.log(hubConnection.json.stringify({ "field": ["a", "b"] }));
        console.log(hubConnection.json.stringify([]));
        console.log(hubConnection.json.stringify(["a"]));
        console.log(hubConnection.json.stringify(["a","b"]));
        console.log("endTest");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment