SIGNALR using Configuration

Source: Internet
Author: User

  1. Add component packages with NuGet Manager install-package microsoft.aspnet.signalr-version 2.1.0//(-version 2.1.0 Optional, No default to the latest version) after the success: will be in the reference more references, scripts folder under the JS file to be used, there is a packages.config file.

    1 <Packages>2       < PackageID= "JQuery"version= "1.6.4"targetframework= "NET40" />3       < PackageID= "Microsoft.AspNet.SignalR"version= "2.1.0"targetframework= "NET40" />4       < PackageID= "Microsoft.AspNet.SignalR.Client"version= "2.1.0"targetframework= "NET40" />5       < PackageID= "Microsoft.AspNet.SignalR.Core"version= "2.1.0"targetframework= "NET40" />6       < PackageID= "Microsoft.AspNet.SignalR.JS"version= "2.1.0"targetframework= "NET40" />7       < PackageID= "Microsoft.AspNet.SignalR.Owin"version= "1.2.1"targetframework= "NET40" />8       < PackageID= "Microsoft.AspNet.SignalR.SystemWeb"version= "2.1.0"targetframework= "NET40" />9       < PackageID= "Microsoft.owin"version= "2.0.1"targetframework= "NET40" />Ten       < PackageID= "Microsoft.Owin.Host.SystemWeb"version= "2.0.1"targetframework= "NET40" /> One       < PackageID= "Microsoft.Owin.Security"version= "2.0.1"targetframework= "NET40" /> A       < PackageID= "Newtonsoft.json"version= "5.0.1"targetframework= "NET40" /> -       < PackageID= "Owin"version= "1.0"targetframework= "NET40" /> -     </Packages>

    Note: The Web. config configuration version here is important.

    1. NET Framework 4.0
    For the. NET client-2.0.2 http://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client/2.0.2
    pm> install-package microsoft.aspnet.signalr.client-version 2.0.2
    For the Server-1.2.0 http://www.nuget.org/packages/Microsoft.AspNet.SignalR/1.2.0
    pm> install-package microsoft.aspnet.signalr-version 1.2.0

    2. NET Framework 4.5
    For the. NET client-2.1.0 https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client/2.1.0
    Pm> Install-package Microsoft.AspNet.SignalR.Client
    For the Server-2.1.0 https://www.nuget.org/packages/Microsoft.AspNet.SignalR/2.1.0
    Pm> Install-package Microsoft.AspNet.SignalR

  2. 1 <system.web>2   <compilationDebug= "true"targetframework= "4.5" />3   <HttpRuntimetargetframework= "4.5" />4 </system.web>5 <system.webserver>6   <Validationvalidateintegratedmodeconfiguration= "false" />7   <Modulesrunallmanagedmodulesforallrequests= "true">8   </Modules>9 </system.webserver>
  3. create route class: Startup.cs
     [Assembly:owinstartup (typeof   (Demo.startup)]  namespace   Demo {  public  class   Startup { public  void   Configuration (Iappbuilder app) {app.    MAPSIGNALR (); SignalR  2.1 . 0  //  App.  Maphubs (); SignalR 1.2.0   

    or Global.asax.cs in Applicatio N_start inside add RouteTable.Routes.MapHubs ();

  4. When referring to the background hub class in JS, the Camel naming method is enabled, so the first letter of the class name is lowercase.

    var noticehub =/*   
    corresponding to the background NoticeHub.cs class, you can rename the class name, add [Hubname ("Noticehub") above the class name, then the foreground is called as follows: */ var noticehub = $.connection. Noticehub;
  5. Inside the background class, through var context = globalhost.connectionmanager.gethubcontext<messengerhub> ();
    Get the Hub object.

  6. Add Reference
    <script type= "Text/javascript" src= "Scripts/jquery-1.6.4.js" ></script>
    <script type= "Text/javascript" src= "Scripts/jquery.signalr-1.2.0.js" ></script>
    Refer to the hub can only put the last, if you are prompted to find/signalr/hubs after the release, you can now run normal local time, go to the site/signalr/hubs copy the content stored in the new hub.js inside, reference hub.js is the same effect.
    <%--<script type= "Text/javascript" src= "/signalr/hubs" ></script>--%>
    <script src= "Scripts/hub.js" type= "Text/javascript" ></script>

  7. Client-side code:
    1<script>2$(function () {3       varNoticehub =$.connection.noticehub;4NOTICEHUB.CLIENT.SENDMSG =function(msg) {alert (msg);};5 6$.connection.hub.start (). Done (function () {7$ ("#spText"). HTML ("function initialization complete!") ");8       });9Or//$.connection.hub.start ();Ten  One     }); A</script> - Server-side code: -<script> the$(function () { -       varNoticehub =$.connection.noticehub; - $.connection.hub.start (); -  +$ ("#btnSend"). Click (function () { -           varmsg = $ ("#txtMsg"). Val (); + NoticeHub.server.sendNotice (msg); A       }); at     }); -</script>

    7. Cross-domain calls:

    varConnection =NewHubconnection ("http://localhost:14251/"); SignalRConsole.SignalR.MessengerHub.Message Message=NewSignalR.MessengerHub.Message (); Message. Content="Moo"; Message. Duration= -; Message. Title="Hello Title"; varMyhub = connection. Createhubproxy ("Messenger"); Connection. Start ().  Wait (); Object[] MyData= {(Object) message,"Sourcing" }; Myhub.invoke ("Broadcastmessage", MyData); Connection. Dispose (); //It's important, otherwise it's easy to fall

SIGNALR using Configuration

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.