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 SignalRSelfHost+MVC and other multi-end messages for push service

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to use SignalRSelfHost+MVC and other multi-end messages for push service, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

I. Overview

Recently, the project is really too busy, and my body is a little uncomfortable. I suffered from chronic pharyngitis. I was out of breath when I slept last night. I didn't have a good rest, and I didn't have much time to write a blog. Today, when my friend asked me when I could send a webside message, I was still busy changing the project. I took advantage of lunch and lunch break to make up for the article I owed before.

The main purpose of this chapter is to realize the communication of multi-end messages to each other, such as sending information to the control side of the simple demo,mvc, and so on.

Create a web client

1. Create a new WebClient solution

2. Create a new project called Clinet mvc under the WebClient solution

3. Choose the corresponding project template according to your actual situation. Here, in order to demonstrate, select the Internet application

4. Sign in the package manager console in vs, and enter the following code

Install-Package Microsoft.AspNet.SignalR.JS

5. Modify the Index.cshtml file code in the Homge folder under Views in the project, as follows

@ {Layout = null;} process demonstration $(function () {$. Connection.hub.url = 'http://localhost:10086/signalr'; var work = $.connection.IMHub; $(' # displayname') .val (prompt ('enter nickname:',')); $('# thisname') .text ('current user:' + $('# displayname') .IMHub ()) Var fromUser = $('# displayname'). Val (); / / corresponding to the backend SendMessage function, message receiving function work.client.receivePrivateMessage = function (user, message) {/ / alert (message); $('# messgaeInfo') .append (message +');}; / / loginUser callback work.client.onConnected = function (connnectId, userName, OnlineUsers) {reloadUser (OnlineUsers);} / / hub connection opens $.connection.hub.start () .done (function () {var username = $('# displayname'). Val (); / / send online message work.server.register (username); / / Click the button to send the message $('# send') .click (function () {var friend = $('# username') .val ()) / / call the backend function to send the specified message work.server.sendPrivateMessage (friend, $("# message"). Val ();}); / / reload the user list var reloadUser = function (userlist) {$("# username"). Empty (); for (I = 0; I

< userlist.length; i++) { $("#username").append("" + userlist[i].UserName + ""); } } 6、在运行我们的web Client项目前,先运行我们前面两章中的serve控制台项目,然后F5运行web Client项目,见如下界面,输入用户名为a的用户登录

7. Then open another browser, as in the previous step, enter b user to log in, a user selects b in the drop-down item (the drop-down option automatically adds a new user when a user logs in), b user selects a, and sends messages to each other for testing, the results are as follows

8. If there is no problem with the message transmission between an and b, which proves that the message transmission on the web side is successful, open the Client console project in our previous chapter, run the Clinet.exe in the bin under this project, and sign in two programs, and enter the login names c and b

9. Use c user to send a message hello a, and the effect is as follows

10. Use c users to send messages to b users hello b! The effect is as follows, which proves that the message sent from the control to the web is successful.

11. Send a message to user c with user d, as shown in the following figure, which proves that the message on the control side is sent successfully

12. Send a message to c with a, as shown in the following figure

13. Send a message to d with b, as shown in the following figure, which proves that web-- > control sent the message successfully

As shown above, it is proved that the owin service made of SignalR can realize multi-terminal communication, and the communication service made by SignalR alone can be separated and decoupled from other projects.

In the later stage, I will continue to optimize and integrate RabbitMQ, etc.

After reading the above, have you mastered how to use SignalRSelfHost+MVC and other multi-end messages to push the service? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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