Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the console program to call the SignalR server to push information

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to implement the console program to call the SignalR server to push information, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.

I. Overview

The console program calls the SignalR server to implement the push message. Since we intended to do the approval message push, our demo direction is to send the message to the designated person. As for chat messages and global broadcasts, there is no demonstration here.

Create 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) {/ / connected successfully, call the Register method chatHub.Invoke ("Register", clientName);}}) / / client receiving implementation, either using js or backend receiving var broadcastHandler = chatHub.On ("receivePrivateMessage", (name, message) = > {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 the solution-- > Select Properties-- > Startup Project-- > multiple Startup projects-- set Clinet and Server to start, as follows:

5. After F5 runs, open a Clinet, enter the user name an in the first Client, display the ID of login an in the Server after login, enter the user name of b in another Clinet, and show the ID of login b in the Server after login. Then, according to the prompt, the person who receives the information in a Clinet is b. After entering hello and pressing Enter, b Client displays the information entered by a, as shown below:

Since the console calls the SignalR server, the Demo for message push is completed.

Thank you for reading this article carefully. I hope the article "how to realize the console program calling the SignalR server to push information" shared by the editor is helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report