Use signalr to implement Asp.net push mode

Source: Internet
Author: User

Display Interface

Open three such interfaces, and then upload the sending information to other interfaces to obtain the data at the same time.

1. Upload signalr jquery. signalr. js and reference it.

2. Create an MVC project.

3. Add the zookeeper category to global. asax, and add the zookeeper category to the category.

Protected void application_start ()
{
Routetable. routes. mapconnection <myconnection> ("Echo", "Echo/{* operation }");
}

4. Create myconnection and accept persistentconnection

In this example, clientid is the user ID information for sending data. It can be specified for a specific user or a group of users.

Protected override task onreceivedasync (string clientid, string data)
{
// Broadcast data to all clients

// Perform data processing here
Data = string. Format ("data volume: {0} Time: {1}", Data, datetime. Now. tostring ());
Return connection. Broadcast (data );
}

The token generation is as follows:

View code

  Public   class   myconnection: persistentconnection {  protected   override  task onreceivedasync ( string  clientid,  string   data) { ///   broadcast data to all clients  DATA =  string . format ( "  the data volume is: {0} Time: {1}   " , Data, datetime. now. tostring ();   return   connection. broadcast (data) ;} 

5. Use the index. cshtml labels

View code

<SCRIPT type = "  Text/JavaScript  " > $ (Function (){  VaR Connection = $. Connection ( '  Echo  '  ); Connection. Received (function (data) {$ (  '  # Messages  ' ). Append ('  <Li>  ' + Data + '  </LI>  '  ) ;}); Connection. Start (); $ (  "  # Broadcast  "  ). Click (function () {connection. Send ($ (  '  # Msg  '  ). Val ());});}); </SCRIPT>

The interface proxy is as follows:

View code

 @ {Viewbag. Title = "  Home Page  "  ;} <SCRIPT type = "  Text/JavaScript  " > $ (Function (){  VaR Connection = $. Connection ( '  Echo  ' ); Connection. Received (function (data) {$ (  '  # Messages  ' ). Append ( '  <Li>  ' + Data + '  </LI>  '  ) ;}); Connection. Start (); $ (  "  # Broadcast  " ). Click (function () {connection. Send ($ (  '  # Msg  '  ). Val () ;}); $ (  "  # Btnstop  "  ). Click (function () {connection. Stop ();});}); </SCRIPT> <H2> @ viewbag. Message </H2> <input type = "  Text  " Id = "  MSG " /> <Input type = "  Button  " Id = "  Broadcast  " Value = "  Sending  " /> <Input type = "  Button  " Id = "  Btnstop  " Value = "  Stop  " /> <Ul id = "  Messages  " > </Ul>

Start and Stop.

VaR connection = $. Connection ('echo '); obtain response

Invalid Response Data:

Connection. Received (function (data ){
$ ('# Messages'). append ('<li>' + Data + '</LI> ');
});
Start connection: connection. Start ();

Stop disconnect: connection. Stop ();

Under the source region:Http://files.cnblogs.com/Teco/MvcApplicationSignalR.rar

The above-mentioned Token Generation: I canceled return connection. Broadcast (data );

Only send to yourself. If you want to send all, useReturn connection. Broadcast (data );

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.