SignalR real-time push and signalr real-time push
The implementation mechanism of SignalR is similar to that of. net wcf or Remoting. It is implemented using remote proxy. There are two interfaces for different purposes:PersistentConnectionAndHubsPersistentConnection implements long-time Javascript round robin (similar to Comet). Hub is used to solve the problem of real-time information exchange. It is implemented by using Javascript dynamic loading and execution methods. SignalR encapsulates the entire connection and the information exchange process is very beautiful. the client and the server use JSON to exchange data.
The following describes the entire process of using the Hubs interface:
1. Define the corresponding hub class on the server side;
2. Define the proxy class corresponding to the hub class on the client;
3. establish a connection between the client and the server );
4. Then the client can call the proxy object method to call the server method, that is, send the request to the server;
5. After receiving the request, the server can send messages to A/group client or all clients (broadcast.
The final implementation:
One is push Information Server (I will build it into a windows service later), one is push information client, and the other is WebClient;
Solution 1 and solution 2? Why do we need to build two solutions? I am afraid some people will be confused.
Solution 1:
SignalR. Lib puts some class libraries of SignalR (both Client and Server reference this project). As for the purpose, you know! What kind of DLL needs to be installed, use nuget to go down
Through the Nuget Console
PM>Install-Package Microsoft. AspNet. SignalR. SelfHost
PM>Install-Package Microsoft. Owin. Cors
PM> Install-Package Microsoft. AspNet. SignalR. Client
Note: During installation, you may encounter an error about the version between dll and. net freamwork.
Open the Server first:
Task. Run () => srv. Start (ServerURL ))
The myHub class must inherit the Hub class
You need to first execute the asynchronous connection method:
private async void ConnectAsync() { try { string url = "http://localhost:8080/signalr"; HubConnection Connection = new HubConnection(url); HubProxy = Connection.CreateHubProxy("MyHub"); await Connection.Start(); } catch (Exception ex) { return; } }
Solution 2: The site you want to push (remember to configure the host)
Js Code to be referenced on the mvc page
<Script src = "~ /Scripts/jquery-1.7.1.min.js "> </script>
<Script src = "~ /Scripts/art. dialog4.1.6/artDialog. js? Skin = green "> </script>
<Script src = "~ /Scripts/art. dialog4.1.6/plugins/iframeTools. js "> </script>
<Script src = "~ /Scripts/jquery. signalR-2.2.0.min.js "> </script>
<Script src = "http: // localhost: 8080/signalr/hubs"> </script>
<Script type = "text/javascript"> $ (function () {$. connection. hub. url = "http: // localhost: 8080/signalr"; var chat = $. connection. myHub; chat. client. addMessage = function (text) {art. dialog ({title: 'push information', content: text}) ;}$. connection. hub. start (); // $. connection. hub. start (). done (function () {// alert ("end"); //}) </script>
SignalR communication model: (reference address: http://www.cnblogs.com/wintersun/p/4148223.html)
The benefits of real-time push will not be mentioned. It is better than the previous method of frequently flushing databases! Because I want to make it a push to online users and some special users, this is just a prototype. I need a Demo partner to send me an insite email!
Today, I did not wash my face or brush my teeth! I watched a movie and wrote a signalR Demo! This is not good. I will go to the crawler tomorrow!