[Solution] Using ASP. NET SignalR to add real-time Web, solutionsignalr
ASP. NET SignalR is a library provided by ASP. NET developers. It simplifies the process for developers to add real-time Web functions to applications. The real-time Web function refers to a function that allows the server code to push content immediately when the connected client becomes available, rather than waiting for the server to wait for the client to request new data.
Official Website: http://signalr.net/
Download: install-package Microsoft. AspNet. SignalR
This section briefly introduces
- Implementation Principle
- Hello World
- Quick analysis
- Notes
Implementation Principle
Based on the implementation principle of SignalR (SR), the SR is fully compatible with the client browser IE8 or later. The browser that fully supports jQuery 1.6.4 can support SignalR.
Hello World
Create an empty Asp. Net Project
Install
Install-package Microsoft. AspNet. SignalR
Install-package bootstrap
Add a hub class
public class ChatHub : Hub { public void Send(string name, string message) { // Call the broadcastMessage method to update clients. Clients.All.broadcastMessage(name, message); } }
Add an OWIN Startup class
[Assembly: OwinStartup (typeof (SignalRChart. startup)] namespace SignalRChart {public class Startup {public void Configuration (IAppBuilder app) {// For more information about how to configure the application, visit the http://go.microsoft.com/fwlink? LinkID = 316888 app. MapSignalR ();}}}
Upload an index.html
1. Import js
<!--Script references. --> <!--Reference the jQuery library. --> <script src="Scripts/jquery-1.9.1.min.js"></script> <!--Reference the SignalR library. --> <script src="Scripts/jquery.signalR-2.2.0.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="signalr/hubs"></script>
2. hub
// Declare a proxy to reference the hub.var chat = $.connection.chatHub;// Call the Send method on the hub.chat.server.send(name, message); // Create a function that the hub can call to broadcast messages.chat.client.broadcastMessage = function (name, message) {}
Quick analysis
Hub code
Client code
Notes
In ASP. net mvc 4 you can do the following:
<Script src = "~ /Signalr/hubs "> </script>
If you're writing an ASP. net mvc 3 application, make sure that you are using Url. Content for your script references:
<Script src = "@ Url. Content ("~ /Signalr/hubs ")"> </script>
Code download: Waiting for sorting
Author: Never, C
Link: http://www.cnblogs.com/neverc/p/4617488.html