Asp.net SignalR simplifies real-time communication. 2. Ship SignalR to the console, and signalr becomes simple.

Source: Internet
Author: User

Asp.net SignalR simplifies real-time communication. 2. Ship SignalR to the console, and signalr becomes simple.

In Asp.net SignalR, real-time communication becomes simple. Siming shares a SignalR directly hosted on IIS. SignalR can also host in any application, including the console, client programs and Windows Services. Mono is also supported, which means that it can be deployed across platforms in Linux.

In my reference blog [SignalR of Asp.net Development Series] Topic 1: article 4 of Asp.net SignalR Quick StartHow to Use Asp.net SignalR in a desktop programThat is, boarding SignalR In the WPF client. I have already personally experienced it and it is quite helpful. If you want to know it, you can study it. Today, I will share with you how to directly host SignalR on the console.

1. Create a console application

2. Right-click the project you created and choose manage NuGet packages --> SearchMicrosoft. Owin. CorsAnd install --> SearchMicrosoft. AspNet. SignalR. SelfHostAnd install

  

To be installedMicrosoft. AspNet. SignalR. SelfHostThe following figure shows readme.txt automatically displayed in. It details how to host SignalR to the console.

3rd, we can follow the instructions in readme.txt. Modify Program. cs as follows:

using Microsoft.AspNet.SignalR;using Microsoft.Owin.Cors;using Microsoft.Owin.Hosting;using Owin;using System;namespace SelfSignalRSvc2._0{    public class Program    {        static void Main(string[] args)        {            // This will *ONLY* bind to localhost, if you want to bind to all addresses            // use http://*:8080 or http://+:8080 to bind to all addresses.             // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx             // for more information.            using (WebApp.Start<Startup>("http://localhost:118/"))            {                Console.WriteLine("Server running at http://localhost:118/");                Console.ReadLine();            }        }    }    public class Startup    {        public void Configuration(IAppBuilder app)        {            app.Map("/signalr", map =>            {                // Setup the cors middleware to run before SignalR.                // By default this will allow all origins. You can                 // configure the set of origins and/or http verbs by                // providing a cors options with a different policy.                map.UseCors(CorsOptions.AllowAll);                var hubConfiguration = new HubConfiguration                {                    // You can enable JSONP by uncommenting line below.                    // JSONP requests are insecure but some older browsers (and some                    // versions of IE) require JSONP to work cross domain                    // EnableJSONP = true                };                // Run the SignalR pipeline. We're not using MapSignalR                // since this branch is already runs under the "/signalr"                // path.                map.RunSignalR(hubConfiguration);            });        }    }}

Create a ChatHub class, inherit from the Hub, and implement the following code:

Using Microsoft. aspNet. signalR; using System. diagnostics; using System. threading. tasks; namespace SelfSignalRSvc {public class ChatHub: hub {// <summary> /// server code for the client to call /// </summary> /// <param name = "message"> </param> public void Send (string message) {var name = Context. connectionId; // call the sendMessage method Clients of all Clients. all. sendMessage (name, message );} /// <summary> // called when the client is connected /// </summary> /// <returns> </returns> public override Task OnConnected () {Trace. writeLine ("client connection successful:" + Context. connectionId); return base. onConnected ();}}}

4. According to the above operations, we have completed the development of the script service. Run VS directly. The console is started successfully.

5. Create an html code to verify whether the script is available.

Remember to reference the jquery-1.10.2.js and jquery. signalR-2.2.2.min.js while modifying the link to the script http: // localhost: 118/signalr/hubs and

The host service address must be specified.$. Connection. hub. url = 'HTTP: // localhost: 118/signalr ';

<! DOCTYPE html> 

You can directly run the html page of the web site and open multiple web pages to receive the same message .. As follows:

 

By boarding signalR to the WPF client, you can see that the two implementations are similar. Therefore, in actual use, you can select an implementation method that suits your needs.

The code word is not easy. Please indicate the source for reprinting.

 

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.