Microsoft Official Example Address: Http://www.asp.net/signalr/overview/deployment/tutorial-signalr-self-host
1. Description:
The SIGNALR server can make an ASP. NET program, or it can be a console or a service program that does not need to be hosted on IIS again. This is what this article is about.
2. Installation extension:
Using the NuGet console Install-Package Microsoft.AspNet.SignalR.SelfHost自托管服务端所需要的程序集。
:
- package microsoft owin cors required for cross-domain access, SIGNALR server is not on the same domain as the client.
3、新建(控制台项目):
① new Startup class, the first sentence to be set for cross-domain
App. Usecors (Corsoptions.allowall); app. MAPSIGNALR ();
② creates a new hub class that defines the send push method, in which the client-defined method is called.
Public class Myhub:hub { void Send (string name, String message) { Clients.All.addMessage (name, message); } }
③ starting the SIGNALR server in the Main method
String url = "http://localhost:8080"; using (Webapp.start (URL)) { Console.WriteLine ("Server running on {0}", URL); Console.ReadLine (); }
4. New (Web project):
In this project, you will use the SIGNALR server that you obtained in the previous step to push the data to the client.
Note the points:
① The hub address at this point is the URL address defined in the previous step.
<script src= "Scripts/jquery.signalr-2.1.0.min.js" ></script><script src= "http://localhost:8080/ Signalr/hubs "></script>
② The hub address that you want to connect to is consistent with the previous step, and the hub class name is used here with the first letter lowercase.
$.connection.hub.url = "HTTP://LOCALHOST:8080/SIGNALR";
$(function () { //Set The Hubs URL for the connection$.connection.hub.url = "HTTP://LOCALHOST:8080/SIGNALR"; //Declare A proxy to reference the hub. varChat =$.connection.myhub; //Create a function, the hub can call to broadcast messages.Chat.client.addMessage =function(name, message) {//Html encode display name and message. varEncodedname = $ (' <div/> '). Text (name). HTML (); varencodedmsg = $ (' <div/> '). Text (message). HTML (); //ADD the message to the page.$ (' #discussion '). Append (' <li><strong> ' +Encodedname+ ' </strong>: ' + encodedmsg + ' </li> '); }; //Get the user name and store it to prepend to messages.$ (' #displayname '). Val (Prompt (' Enter your Name: ', ' ")); //Set initial focus to message input box.$ (' #message '). focus (); //Start the connection.$.connection.hub.start (). Done (function () { $(' #sendmessage '). Click (function () { //Call the Send method on the hub.Chat.server.send ($ (' #displayname '). Val (), $ (' #message '). Val ()); //Clear text box and reset focus for next comment.$ (' #message '). Val (' '). focus (); }); }); });
5, after the build, start the SIGNALR server first.
Create a self-hosted SIGNALR server