SignalR Self Host + MVC and other multi-terminal message push services (2), signalrmvc

Source: Internet
Author: User

SignalR Self Host + MVC and other multi-terminal message push services (2), signalrmvc

I. Overview

In the previous article, we implemented the SignalR self-managed server. Today, we use the console program to call the SignalR server to push information. Because we were planning to push approval messages, therefore, our demo is intended to send messages to designated persons. As for chat messages and global broadcasts, we will not demonstrate them here.

2. Create a console client

1. Create a 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 ("Enter the 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) {// The connection is successful. Call the Register Method chatHub. invoke ("Register", clientName) ;}}); // The client receives the code. You can use js or the backend to receive the var broadcastHandler = chatHub. on <string, string> ("paieprivatemessage", (name, message) =>{ Console. writeLine ("[{0}] {1 }:{ 2}", DateTime. now. toString ("HH: mm: ss"), name, message) ;}); Console. writeLine ("Enter the receiver name:"); var _ name = Console. readLine (); Console. writeLine ("Enter the message to send! "); While (true) {var _ message = Console. readLine (); chatHub. invoke ("SendPrivateMessage", _ name, _ message ). continueWith (t => {if (t. isFaulted) {Console. writeLine ("connection failed! ") ;}}); Console. WriteLine (" Enter the receiver name: "); _ name = Console. ReadLine (); Console. WriteLine (" Enter the message! ");}}}}

4. Right-click solution> select Properties> Start Project> multi-Start Project> set Clinet and Server to start, as shown below:

5. After F5 is run, open another Clinet. In the first Client, enter the user name as a. After logon, the Server displays the ID of Logon a. In the other Clinet, enter the user name as B, after logging on to the Server, the ID of Logon B is displayed. Then, according to the prompt, B is the person who enters the received information in a Clinet. After Entering hello, press Enter, B Client displays the information entered by a, such:

Since then, the console has called the SignalR server, and the Demo for message pushing has been completed. This is what we will write today. In the next chapter, we will implement the call of the SignalR service on the B/S end to push messages.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.