[Solution] Using ASP. NET SignalR to add real-time Web, solutionsignalr

Source: Internet
Author: User

[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.