From 5cabbcbc63d4bb68f1acdb7cfaab77e86db596ba Mon Sep 17 00:00:00 2001 From: David Fowler Date: Wed, 4 Jul 2012 01:32:52 -0700 Subject: [PATCH] Make event names and proxy names case insensitive. Fixes #508 --- SignalR.Client.JS/jquery.signalR.hubs.js | 28 +++++++++++++------ .../Hubs/MouseTracking/MouseTracking.js | 6 ++-- .../Scripts/jquery.signalR.js | 28 +++++++++++++------ .../Scripts/jquery.signalR.min.js | 2 +- .../Content/Scripts/jquery.signalR.js | 28 +++++++++++++------ .../Content/Scripts/jquery.signalR.min.js | 2 +- 6 files changed, 65 insertions(+), 29 deletions(-) diff --git a/SignalR.Client.JS/jquery.signalR.hubs.js b/SignalR.Client.JS/jquery.signalR.hubs.js index 15943389d5..4a7f555a1d 100644 --- a/SignalR.Client.JS/jquery.signalR.hubs.js +++ b/SignalR.Client.JS/jquery.signalR.hubs.js @@ -3,7 +3,7 @@ (function ($, window) { "use strict"; - + // we use a global id for tracking callbacks so the server doesn't have to send extra info like hub name var callbackId = 0, callbacks = {}; @@ -49,6 +49,10 @@ /// The name of the hub event to register the callback for. /// The callback to be invoked. var self = this; + + // Normalize the event name to lowercase + eventName = eventName.toLowerCase(); + $(self).bind(eventName, function (e, data) { callback.apply(self, data); }); @@ -58,7 +62,7 @@ invoke: function (methodName) { /// Invokes a server hub method with the given arguments. /// The name of the server hub method. - + var self = this, args = $.makeArray(arguments).slice(1), userCallback = args[args.length - 1], // last argument @@ -128,12 +132,12 @@ // Wire up the received handler connection.received(function (data) { - var proxy, dataCallbackId, callback; + var proxy, dataCallbackId, callback, hubName, eventName; if (!data) { return; } - if (typeof(data.Id) !== "undefined") { + if (typeof (data.Id) !== "undefined") { // We received the return value from a server method invocation, look up callback by id and call it dataCallbackId = data.Id.toString(); callback = callbacks[dataCallbackId]; @@ -146,11 +150,15 @@ callback.method.call(callback.scope, data); } } else { + // Normalize the names to lowercase + hubName = data.Hub.toLowerCase(); + eventName = data.Method.toLowerCase(); + // We received a client invocation request, i.e. broadcast from server hub // Trigger the local invocation event - proxy = this.proxies[data.Hub]; + proxy = this.proxies[hubName]; $.extend(proxy.state, data.State); - $(proxy).trigger(data.Method, [data.Args]); + $(proxy).trigger(eventName, [data.Args]); } }); }; @@ -163,6 +171,10 @@ /// /// The name of the hub on the server to create the proxy for. /// + + // Normalize the name to lowercase + hubName = hubName.toLowerCase(); + var proxy = this.proxies[hubName]; if (!proxy) { proxy = hubProxy(this, hubName); @@ -170,9 +182,9 @@ } return proxy; }; - + hubConnection.fn.init.prototype = hubConnection.fn; $.hubConnection = hubConnection; -}(window.jQuery, window)); \ No newline at end of file +} (window.jQuery, window)); \ No newline at end of file diff --git a/samples/SignalR.Hosting.AspNet.Samples/Hubs/MouseTracking/MouseTracking.js b/samples/SignalR.Hosting.AspNet.Samples/Hubs/MouseTracking/MouseTracking.js index be245f0c8e..e3d8fa3c56 100644 --- a/samples/SignalR.Hosting.AspNet.Samples/Hubs/MouseTracking/MouseTracking.js +++ b/samples/SignalR.Hosting.AspNet.Samples/Hubs/MouseTracking/MouseTracking.js @@ -3,9 +3,9 @@ $(function () { var hubConnection = $.hubConnection(), - hub = hubConnection.createProxy('MouseTracking'); + hub = hubConnection.createProxy('mouseTracking'); - hub.on('moveMouse', function (id, x, y) { + hub.on('MoveMouse', function (id, x, y) { if (id == this.state.id) { return; } @@ -27,7 +27,7 @@ $(function () { hubConnection.start({ transport: activeTransport }) .done(function () { - return hub.invoke('Join'); + return hub.invoke('join'); }) .done(function () { $(document).mousemove(function (e) { diff --git a/samples/SignalR.Hosting.AspNet.Samples/Scripts/jquery.signalR.js b/samples/SignalR.Hosting.AspNet.Samples/Scripts/jquery.signalR.js index 7deba3acdf..3bc12206a5 100644 --- a/samples/SignalR.Hosting.AspNet.Samples/Scripts/jquery.signalR.js +++ b/samples/SignalR.Hosting.AspNet.Samples/Scripts/jquery.signalR.js @@ -1290,7 +1290,7 @@ (function ($, window) { "use strict"; - + // we use a global id for tracking callbacks so the server doesn't have to send extra info like hub name var callbackId = 0, callbacks = {}; @@ -1336,6 +1336,10 @@ /// The name of the hub event to register the callback for. /// The callback to be invoked. var self = this; + + // Normalize the event name to lowercase + eventName = eventName.toLowerCase(); + $(self).bind(eventName, function (e, data) { callback.apply(self, data); }); @@ -1345,7 +1349,7 @@ invoke: function (methodName) { /// Invokes a server hub method with the given arguments. /// The name of the server hub method. - + var self = this, args = $.makeArray(arguments).slice(1), userCallback = args[args.length - 1], // last argument @@ -1415,12 +1419,12 @@ // Wire up the received handler connection.received(function (data) { - var proxy, dataCallbackId, callback; + var proxy, dataCallbackId, callback, hubName, eventName; if (!data) { return; } - if (typeof(data.Id) !== "undefined") { + if (typeof (data.Id) !== "undefined") { // We received the return value from a server method invocation, look up callback by id and call it dataCallbackId = data.Id.toString(); callback = callbacks[dataCallbackId]; @@ -1433,11 +1437,15 @@ callback.method.call(callback.scope, data); } } else { + // Normalize the names to lowercase + hubName = data.Hub.toLowerCase(); + eventName = data.Method.toLowerCase(); + // We received a client invocation request, i.e. broadcast from server hub // Trigger the local invocation event - proxy = this.proxies[data.Hub]; + proxy = this.proxies[hubName]; $.extend(proxy.state, data.State); - $(proxy).trigger(data.Method, [data.Args]); + $(proxy).trigger(eventName, [data.Args]); } }); }; @@ -1450,6 +1458,10 @@ /// /// The name of the hub on the server to create the proxy for. /// + + // Normalize the name to lowercase + hubName = hubName.toLowerCase(); + var proxy = this.proxies[hubName]; if (!proxy) { proxy = hubProxy(this, hubName); @@ -1457,9 +1469,9 @@ } return proxy; }; - + hubConnection.fn.init.prototype = hubConnection.fn; $.hubConnection = hubConnection; -}(window.jQuery, window)); +} (window.jQuery, window)); diff --git a/samples/SignalR.Hosting.AspNet.Samples/Scripts/jquery.signalR.min.js b/samples/SignalR.Hosting.AspNet.Samples/Scripts/jquery.signalR.min.js index c238050388..ffccda279e 100644 --- a/samples/SignalR.Hosting.AspNet.Samples/Scripts/jquery.signalR.min.js +++ b/samples/SignalR.Hosting.AspNet.Samples/Scripts/jquery.signalR.min.js @@ -6,4 +6,4 @@ * Licensed under the MIT. * https://github.com/SignalR/SignalR/blob/master/LICENSE.md */ -(function(n,t){"use strict";if(typeof n!="function")throw"SignalR: jQuery not found. Please ensure jQuery is referenced before the SignalR.js file.";if(!t.JSON)throw"SignalR: No JSON parser found. Please ensure json2.js is referenced before the SignalR.js file if you need to support clients without native JSON parsing support, e.g. IE<8.";var i,f,r={onStart:"onStart",onStarting:"onStarting",onSending:"onSending",onReceived:"onReceived",onError:"onError",onReconnect:"onReconnect",onStateChanged:"onStateChanged",onDisconnect:"onDisconnect"},o=function(n,i){if(i!==!1){var r;typeof t.console!="undefined"&&(r="["+(new Date).toTimeString()+"] SignalR: "+n,t.console.debug?t.console.debug(r):t.console.log&&t.console.log(r))}},s=function(i){var r;return(i=n.trim(i),i.indexOf("http")!==0)?!1:(r=t.document.createElement("a"),r.href=i,r.protocol+r.host!==t.location.protocol+t.location.host)},u=function(t,i,u){return i===t.state?(n(t).trigger(r.onStateChanged,[{oldState:t.state,newState:u}]),t.state=u,!0):!1},e=function(n){return n.state===i.connectionState.disconnected};i=function(n,t,r){return new i.fn.init(n,t,r)},i.events=r,i.changeState=u,i.isDisconnecting=e,i.connectionState={connecting:0,connected:1,reconnecting:2,disconnected:4},i.hub={start:function(){throw"SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g.