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