Skip to content

Commit

Permalink
Auto JSON stringify objects for connections in JS client
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
DamianEdwards committed Jul 3, 2013
1 parent 392fd26 commit b567a0c
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,16 @@
}
},

stringifySend: function (connection, message) {
if ($.type(message) === "string") {
return message;
}
return connection.json.stringify(message);
},

ajaxSend: function (connection, data) {
var url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
var payload = transportLogic.stringifySend(connection, data),
url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
url = this.addQs(url, connection.qs);
return $.ajax(
$.extend({}, $.signalR.ajaxDefaults, {
Expand All @@ -1023,7 +1031,7 @@
contentType: signalR._.defaultContentType,
dataType: connection.ajaxDataType,
data: {
data: data
data: payload
},
success: function (result) {
if (result) {
Expand Down Expand Up @@ -1223,7 +1231,8 @@
supportsKeepAlive: true,

send: function (connection, data) {
connection.socket.send(data);
var payload = transportLogic.stringifySend(connection, data);
connection.socket.send(payload);
},

start: function (connection, onSuccess, onFailed) {
Expand Down Expand Up @@ -2065,7 +2074,7 @@
data.S = that.state;
}

that.connection.send(that.connection.json.stringify(data));
that.connection.send(data);

return d.promise();
},
Expand Down
16 changes: 8 additions & 8 deletions samples/Microsoft.AspNet.SignalR.Samples/Raw/Default.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -124,35 +124,35 @@
start();
$("#send").click(function () {
connection.send(window.JSON.stringify({ type: 0, value: $("#me").val() }));
connection.send({ type: 0, value: $("#me").val() });
});
$("#broadcast").click(function () {
connection.send(window.JSON.stringify({ type: 1, value: $("#msg").val() }));
connection.send({ type: 1, value: $("#msg").val() });
});
$("#broadcast-exceptme").click(function () {
connection.send(window.JSON.stringify({ type: 7, value: $("#msg").val() }));
connection.send({ type: 7, value: $("#msg").val() });
});
$("#join").click(function () {
connection.send(window.JSON.stringify({ type: 2, value: $("#msg").val() }));
connection.send({ type: 2, value: $("#msg").val() });
});
$("#privatemsg").click(function () {
connection.send(window.JSON.stringify({ type: 3, value: $("#user").val() + "|" + $("#message").val() }));
connection.send({ type: 3, value: $("#user").val() + "|" + $("#message").val() });
});
$('#join-group').click(function () {
connection.send(window.JSON.stringify({ type: 4, value: $("#msg").val() }));
connection.send({ type: 4, value: $("#msg").val() });
});
$('#leave-group').click(function () {
connection.send(window.JSON.stringify({ type: 5, value: $("#msg").val() }));
connection.send({ type: 5, value: $("#msg").val() });
});
$("#groupmsg").click(function () {
connection.send(window.JSON.stringify({ type: 6, value: $("#user").val() + "|" + $("#message").val() }));
connection.send({ type: 6, value: $("#user").val() + "|" + $("#message").val() });
});
$("#stopStart").click(function () {
Expand Down
17 changes: 13 additions & 4 deletions samples/Microsoft.AspNet.SignalR.Samples/Scripts/jquery.signalR.js
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,16 @@
}
},

stringifySend: function (connection, message) {
if ($.type(message) === "string") {
return message;
}
return connection.json.stringify(message);
},

ajaxSend: function (connection, data) {
var url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
var payload = transportLogic.stringifySend(connection, data),
url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
url = this.addQs(url, connection.qs);
return $.ajax(
$.extend({}, $.signalR.ajaxDefaults, {
Expand All @@ -1023,7 +1031,7 @@
contentType: signalR._.defaultContentType,
dataType: connection.ajaxDataType,
data: {
data: data
data: payload
},
success: function (result) {
if (result) {
Expand Down Expand Up @@ -1223,7 +1231,8 @@
supportsKeepAlive: true,

send: function (connection, data) {
connection.socket.send(data);
var payload = transportLogic.stringifySend(connection, data);
connection.socket.send(payload);
},

start: function (connection, onSuccess, onFailed) {
Expand Down Expand Up @@ -2065,7 +2074,7 @@
data.S = that.state;
}

that.connection.send(that.connection.json.stringify(data));
that.connection.send(data);

return d.promise();
},
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
data.S = that.state;
}

that.connection.send(that.connection.json.stringify(data));
that.connection.send(data);

return d.promise();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,16 @@
}
},

stringifySend: function (connection, message) {
if ($.type(message) === "string") {
return message;
}
return connection.json.stringify(message);
},

ajaxSend: function (connection, data) {
var url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
var payload = transportLogic.stringifySend(connection, data),
url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
url = this.addQs(url, connection.qs);
return $.ajax(
$.extend({}, $.signalR.ajaxDefaults, {
Expand All @@ -186,7 +194,7 @@
contentType: signalR._.defaultContentType,
dataType: connection.ajaxDataType,
data: {
data: data
data: payload
},
success: function (result) {
if (result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
supportsKeepAlive: true,

send: function (connection, data) {
connection.socket.send(data);
var payload = transportLogic.stringifySend(connection, data);
connection.socket.send(payload);
},

start: function (connection, onSuccess, onFailed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,16 @@
}
},

stringifySend: function (connection, message) {
if ($.type(message) === "string") {
return message;
}
return connection.json.stringify(message);
},

ajaxSend: function (connection, data) {
var url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
var payload = transportLogic.stringifySend(connection, data),
url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
url = this.addQs(url, connection.qs);
return $.ajax(
$.extend({}, $.signalR.ajaxDefaults, {
Expand All @@ -1023,7 +1031,7 @@
contentType: signalR._.defaultContentType,
dataType: connection.ajaxDataType,
data: {
data: data
data: payload
},
success: function (result) {
if (result) {
Expand Down Expand Up @@ -1223,7 +1231,8 @@
supportsKeepAlive: true,

send: function (connection, data) {
connection.socket.send(data);
var payload = transportLogic.stringifySend(connection, data);
connection.socket.send(payload);
},

start: function (connection, onSuccess, onFailed) {
Expand Down Expand Up @@ -2065,7 +2074,7 @@
data.S = that.state;
}

that.connection.send(that.connection.json.stringify(data));
that.connection.send(data);

return d.promise();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,45 @@ testUtilities.runWithAllTransports(function (transport) {
connection.stop();
};
});

QUnit.asyncTimeoutTest(transport + " transport auto JSON encodes messages correctly when sending.", testUtilities.defaultTestTimeout, function (end, assert, testName) {
var connection = testUtilities.createConnection("autoencodedjson", end, assert, testName),
values = [];

connection.received(function (data) {
values.push(data);

if (values.length === 3) {
$.each(values, function (index, val) {
var decoded;
if (val.indexOf("1.") >= 0) {
assert.equal(val, "1.test", "Raw string correctly sent not JSON encoded");
} else if (val.indexOf("2.") >= 0) {
decoded = JSON.parse(val);
assert.equal(decoded.test, "2.test", "Object correctly sent JSON encoded");
} else if (val.indexOf("3.") >= 0) {
decoded = JSON.parse(val);
assert.equal(decoded[0], "3.test", "Array correctly sent JSON encoded");
} else {
// No idea how we got here
assert.fail("Unexpected message returned from server");
}
});
end();
}
});

connection.start({ transport: transport }).done(function () {
connection.send("1.test");
connection.send({ test: "2.test" });
connection.send(["3.test"]);
});

// Cleanup
return function () {
connection.stop();
};
});
});

QUnit.module("Connection Facts", !window.document.commandLineTest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public static void ConfigureRoutes(IAppBuilder app, IDependencyResolver resolver
};

app.MapConnection<MySendingConnection>("multisend", crossDomainConfig);
app.MapConnection<AutoEncodedJsonConnection>("autoencodedjson", crossDomainConfig);

var config = new ConnectionConfiguration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.AspNet.SignalR.Tests.Common.Connections
{
public class AutoEncodedJsonConnection : PersistentConnection
{
protected override Task OnReceived(IRequest request, string connectionId, string data)
{
return Connection.Send(connectionId, data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
</Compile>
<Compile Include="App_Start\Initializer.cs" />
<Compile Include="Connections\AuthenticatedEchoConnection.cs" />
<Compile Include="Connections\AutoJsonEncodedConnection.cs" />
<Compile Include="Connections\FallbackToLongPollingConnectionThrows.cs" />
<Compile Include="Connections\ExamineHeadersConnection.cs" />
<Compile Include="Connections\ExamineReconnectPath.cs" />
Expand Down

0 comments on commit b567a0c

Please sign in to comment.