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 use multi-end message push services such as SignalR Self Host+MVC

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to use SignalR Self Host+MVC and other multi-end message push services, in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

I. Overview

In the last article, we simply implemented the SignalR self-hosting server. Today we are going to implement the console program calling the SignalR server to push messages. As we intended to do approval message push before, our demo direction is to do the designated person to send messages. 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.

This is the answer to the question on how to use SignalR Self Host+MVC and other multi-end message push services. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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