In the previous article, we used a persistent connection, but using a persistent connection, this kind of analog socket is still very inconvenient to use, such as only a single
Onreceived method to handle business logic, such as:
1 protected override Task onreceived (irequest request, String ConnectionID, String data) 2 {3 return Connection.broadcast (data); 4 }
But not in the singlar system, we have a more approachable hub layer like RPC, like this:
is not very curious??? Let's take a look at how this hub layer is built.
One: Create a new hub hub layer
Select the Web Template, there is a Singlar hub class template, new, in this template has a sample code, Clients.All.hello () is used to invoke the client's Hello method,
The hello () in MyHub1 is used to make the client call, so that it implements two-way communication, such as:
Second: Register in startup
In fact, registration is very simple, more than persistent connection is easier, because the persistent connection requires a "class" Map a "url" such a pattern, and the hub is not required, it is a default singlar path
Map the entire hub, such as the hints in:
Three: Implementing the Client
Client implementation, there is a proxy and no agent one said, no agent, we ourselves createproxy, have the agent is to use the dynamic generation of a JS file for programming.
1. Agentless mode
<! DOCTYPE html>
As can be seen from the above, the Invoke method is called the operation of the server method, such as the service side of the Hello method, JS is used to bind the client method, let the server call,
Then we run it:
2. Have proxy mode
There is a proxy mode, you need to dynamically generate a JS, using the script reference.
<script src= "/signalr/js" ></script>
Then run the program, there is automatically generated a JS file, is not very magical ~ ~ ~
With this JS, we can easily program, but there is a regret point is that JS is generated after the program is running, so that in the code when programming is not able to have this dynamic
Into the JS file, the only thing you can do is: Copy the running JS code to the script folder, after adding a reference, you can program in VS, such as:
Well, some people say that this is not very convenient, indeed inconvenient, we also have a more advanced usage, that is, using the Signlar.exe +vs event generated under Singlar.utils, this mode
You can refer to the Weibo in the upper right corner of this page.
OK, here we go, keep busy.
Hub operation for RPC mode