Skip to content
This repository
Browse code

Added consistent way to get a strongly typed representation of the co…

…nnected clients.

- Added IClientProxy interface.
- Renamed *Agent classes to *Proxy
- Added test for interface that invokes a method based on the input.
  • Loading branch information...
commit fa6d0b533e7d0adb35374ce58d4cd74b7473537a 1 parent 64d83d7
David Fowler authored June 27, 2012
22  SignalR.Tests/HubFacts.cs
@@ -230,5 +230,27 @@ public void ComplexPersonState()
230 230
 
231 231
             connection.Stop();
232 232
         }
  233
+
  234
+        [Fact]
  235
+        public void DynamicInvokeTest()
  236
+        {
  237
+            var host = new MemoryHost();
  238
+            host.MapHubs();
  239
+            var connection = new Client.Hubs.HubConnection("http://site/");
  240
+            string callback = "!!!CallMeBack!!!";
  241
+
  242
+            var hub = connection.CreateProxy("demo");
  243
+
  244
+            var wh = new ManualResetEvent(false);
  245
+
  246
+            hub.On(callback, () => wh.Set());
  247
+
  248
+            connection.Start(host).Wait();
  249
+
  250
+            hub.Invoke("DynamicInvoke", callback).Wait();
  251
+
  252
+            Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
  253
+            connection.Stop();
  254
+        }
233 255
     }
234 256
 }
2  SignalR/ConnectionManager.cs
@@ -65,7 +65,7 @@ public IHubContext GetHubContext(string hubName)
65 65
             var hubManager = _resolver.Resolve<IHubManager>();
66 66
             HubDescriptor hubDescriptor = hubManager.EnsureHub(hubName);
67 67
 
68  
-            return new HubContext(new ClientAgent(connection, hubDescriptor.Name), 
  68
+            return new HubContext(new ClientProxy(connection, hubDescriptor.Name), 
69 69
                                   new GroupManager(connection, hubName));
70 70
         }
71 71
 
6  SignalR/Hubs/ClientAgent.cs → SignalR/Hubs/ClientProxy.cs
@@ -3,12 +3,12 @@
3 3
 
4 4
 namespace SignalR.Hubs
5 5
 {
6  
-    public class ClientAgent : DynamicObject
  6
+    public class ClientProxy : DynamicObject, IClientProxy
7 7
     {
8 8
         private readonly IConnection _connection;
9 9
         private readonly string _hubName;
10 10
 
11  
-        public ClientAgent(IConnection connection, string hubName)
  11
+        public ClientProxy(IConnection connection, string hubName)
12 12
         {
13 13
             _connection = connection;
14 14
             _hubName = hubName;
@@ -18,7 +18,7 @@ public ClientAgent(IConnection connection, string hubName)
18 18
         {
19 19
             get
20 20
             {
21  
-                return new SignalAgent(_connection, key, _hubName);
  21
+                return new SignalProxy(_connection, key, _hubName);
22 22
             }
23 23
         }
24 24
 
4  SignalR/Hubs/HubDispatcher.cs
@@ -201,8 +201,8 @@ private IHub CreateHub(IRequest request, HubDescriptor descriptor, string connec
201 201
                 {
202 202
                     state = state ?? new TrackingDictionary();
203 203
                     hub.Context = new HubCallerContext(request, connectionId);
204  
-                    hub.Caller = new StatefulSignalAgent(Connection, connectionId, descriptor.Name, state);
205  
-                    hub.Clients = new ClientAgent(Connection, descriptor.Name);
  204
+                    hub.Caller = new StatefulSignalProxy(Connection, connectionId, descriptor.Name, state);
  205
+                    hub.Clients = new ClientProxy(Connection, descriptor.Name);
206 206
                     hub.Groups = new GroupManager(Connection, descriptor.Name);
207 207
                 }
208 208
 
18  SignalR/Hubs/IClientProxy.cs
... ...
@@ -0,0 +1,18 @@
  1
+using System.Threading.Tasks;
  2
+
  3
+namespace SignalR.Hubs
  4
+{
  5
+    /// <summary>
  6
+    /// A server side proxy for the client side hub.
  7
+    /// </summary>
  8
+    public interface IClientProxy
  9
+    {
  10
+        /// <summary>
  11
+        /// Invokes a method on the connection(s) represented by the <see cref="IClientProxy"/> instance.
  12
+        /// </summary>
  13
+        /// <param name="method">name of the method to invoke</param>
  14
+        /// <param name="args">argumetns to pass to the client</param>
  15
+        /// <returns>A task that represents when the data has been sent to the client.</returns>
  16
+        Task Invoke(string method, params object[] args);
  17
+    }
  18
+}
4  SignalR/Hubs/SignalAgent.cs → SignalR/Hubs/SignalProxy.cs
@@ -3,13 +3,13 @@
3 3
 
4 4
 namespace SignalR.Hubs
5 5
 {
6  
-    public class SignalAgent : DynamicObject
  6
+    public class SignalProxy : DynamicObject, IClientProxy
7 7
     {
8 8
         protected readonly IConnection _connection;
9 9
         protected readonly string _signal;
10 10
         protected readonly string _hubName;
11 11
 
12  
-        public SignalAgent(IConnection connection, string signal, string hubName)
  12
+        public SignalProxy(IConnection connection, string signal, string hubName)
13 13
         {
14 14
             _connection = connection;
15 15
             _signal = signal;
4  SignalR/Hubs/StatefulSignalAgent.cs → SignalR/Hubs/StatefulSignalProxy.cs
@@ -2,11 +2,11 @@
2 2
 
3 3
 namespace SignalR.Hubs
4 4
 {
5  
-    public class StatefulSignalAgent : SignalAgent
  5
+    public class StatefulSignalProxy : SignalProxy
6 6
     {
7 7
         private readonly TrackingDictionary _state;
8 8
 
9  
-        public StatefulSignalAgent(IConnection connection, string signal, string hubName, TrackingDictionary state)
  9
+        public StatefulSignalProxy(IConnection connection, string signal, string hubName, TrackingDictionary state)
10 10
             : base(connection, signal, hubName)
11 11
         {
12 12
             _state = state;
7  SignalR/SignalR.csproj
@@ -61,11 +61,12 @@
61 61
     <Compile Include="Hubs\HubRequest.cs" />
62 62
     <Compile Include="Hubs\HubRequestParser.cs" />
63 63
     <Compile Include="Hubs\HubResponse.cs" />
  64
+    <Compile Include="Hubs\IClientProxy.cs" />
64 65
     <Compile Include="Hubs\IHubContext.cs" />
65 66
     <Compile Include="Hubs\IHubRequestParser.cs" />
66 67
     <Compile Include="Json\IJsonValue.cs" />
67 68
     <Compile Include="Json\JTokenValue.cs" />
68  
-    <Compile Include="Hubs\StatefulSignalAgent.cs" />
  69
+    <Compile Include="Hubs\StatefulSignalProxy.cs" />
69 70
     <Compile Include="Infrastructure\ConnectionExtensions.cs" />
70 71
     <Compile Include="Hubs\Extensions\HubTypeExtensions.cs" />
71 72
     <Compile Include="Hubs\Extensions\HubManagerExtensions.cs" />
@@ -116,11 +117,11 @@
116 117
     <Compile Include="Hubs\DefaultJavaScriptProxyGenerator.cs" />
117 118
     <Compile Include="Hubs\Lookup\ReflectedHubDescriptorProvider.cs" />
118 119
     <Compile Include="Hubs\Hub.cs" />
119  
-    <Compile Include="Hubs\ClientAgent.cs" />
  120
+    <Compile Include="Hubs\ClientProxy.cs" />
120 121
     <Compile Include="Hubs\HubCallerContext.cs" />
121 122
     <Compile Include="Hubs\HubDispatcher.cs" />
122 123
     <Compile Include="Hubs\Lookup\IHubDescriptorProvider.cs" />
123  
-    <Compile Include="Hubs\SignalAgent.cs" />
  124
+    <Compile Include="Hubs\SignalProxy.cs" />
124 125
     <Compile Include="Hubs\NullJavaScriptMinifier.cs" />
125 126
     <Compile Include="Infrastructure\IDependencyResolver.cs" />
126 127
     <Compile Include="Hubs\IJavaScriptMinifier.cs" />
6  samples/SignalR.Hosting.AspNet.Samples/Hubs/DemoHub/DemoHub.cs
@@ -136,6 +136,12 @@ public void TestGuid()
136 136
             Caller.TestGuid(new Guid());
137 137
         }
138 138
 
  139
+        public void DynamicInvoke(string method)
  140
+        {
  141
+            IClientProxy proxy = Caller;
  142
+            proxy.Invoke(method);
  143
+        }
  144
+
139 145
         public class Person
140 146
         {
141 147
             public string Name { get; set; }
6  samples/SignalR.Hosting.AspNet.Samples/Scripts/hubs.js
@@ -171,7 +171,7 @@
171 171
         demo: {
172 172
             _: {
173 173
                 hubName: 'demo',
174  
-                ignoreMembers: ['addToGroups', 'complexArray', 'complexType', 'doSomethingAndCallError', 'dynamicTask', 'genericTaskTypedAsPlain', 'genericTaskWithException', 'getValue', 'multipleCalls', 'overload', 'passingDynamicComplex', 'plainTask', 'readStateValue', 'setStateValue', 'simpleArray', 'taskWithException', 'unsupportedOverload', 'namespace', 'ignoreMembers', 'callbacks'],
  174
+                ignoreMembers: ['addToGroups', 'complexArray', 'complexType', 'doSomethingAndCallError', 'dynamicTask', 'genericTaskTypedAsPlain', 'genericTaskWithException', 'getValue', 'multipleCalls', 'overload', 'passingDynamicComplex', 'plainTask', 'readStateValue', 'setStateValue', 'simpleArray', 'taskWithException', 'testGuid', 'unsupportedOverload', 'namespace', 'ignoreMembers', 'callbacks'],
175 175
                 connection: function () { return signalR.hub; }
176 176
             },
177 177
 
@@ -241,6 +241,10 @@
241 241
 
242 242
             unsupportedOverload: function (x, callback) {
243 243
                 return serverCall(this, "UnsupportedOverload", $.makeArray(arguments));
  244
+            },
  245
+
  246
+            testGuid: function (callback) {
  247
+                return serverCall(this, "TestGuid", $.makeArray(arguments));
244 248
             }
245 249
         },
246 250
         drawingPad: {

0 notes on commit fa6d0b5

Please sign in to comment.
Something went wrong with that request. Please try again.