SignalR self host multi-terminal message Push service introduction (II)

Source: Internet
Author: User
This article is mainly for you to introduce the SIGNALR self HOST+MVC and other multi-terminal message push service of the second article, with a certain reference value, interested in small partners can refer to

I. Overview

In the last article we simply implemented the SIGNALR self-hosted server, today we implement the console program call SIGNALR Server to implement push information, because we are planning to do the approval message push, so our demo direction is to do the designated person to send information, As for chat messages and global broadcasts, this is not a demo.

Ii. Creating a console Client

1. Create a new console named Clinet under the Signalrproject solution

2. Enter the following command in the package management console


Install-package Microsoft.AspNet.SignalR.Client

3. Modify the Program.cs code in the Clinet project


Using microsoft.aspnet.signalr.client;using System;namespace clinet{class Program {static void Main (string[] args)      {Console.Write ("Please enter user name:");      String clientName = Console.ReadLine ();      var url = "http://localhost:10086/";      var connection = new Hubconnection (URL); var Chathub = connection.            Createhubproxy ("Imhub"); Connection. Start (). ContinueWith (T + = {if (!t.isfaulted) {///connection succeeded, call the Register method Chathub.invoke ("Regist        Er ", clientName);      }      }); The client receives the implementation, can use JS, can also use the backend to receive var Broadcasthandler = chathub.on<string, string> ("Receiveprivatemessage", (Name, mess      Age) = {Console.WriteLine ("[{0}]{1}: {2}", DateTime.Now.ToString ("HH:mm:ss"), name, message);      });      Console.WriteLine ("Please enter recipient name:");      var _name = Console.ReadLine ();      Console.WriteLine ("Please enter send message!");        while (true) {var _message = Console.ReadLine (); Chathub.invoke ("Sendprivatemessage", _name, _message).          ContinueWith (T + = {if (t.isfaulted) {Console.WriteLine ("Connection Failed!");        }        });        Console.WriteLine ("Please enter recipient name:");        _name = Console.ReadLine ();      Console.WriteLine ("Please enter send message!"); }    }  }}

4. Right-click Solution--Select Properties--Start Project--Multi-boot project--and set clinet and server to start, as follows:

5, F5 Run, then open a clinet, in the first client entered the user name is a, log in after the server displays the ID of login A, in another clinet enter the user name B, login after the server displays the ID of login B, and then according to the information, a Clinet Enter the person receiving the message B, after entering Hello and pressing ENTER, b client displays the information of a input, such as:

Since this console calls the SIGNALR server, the implementation of the message push demo is complete, today first write to this, the next chapter we implement B/s end call SIGNALR service to push messages

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.