Signalr Information Push and signalr Information Push

Source: Internet
Author: User
Tags sendmsg

Signalr Information Push and signalr Information Push

  Preface

It's almost half a year since I wrote the article last time. I feel more and more lazy, even if I am idle, I don't want to move. I bought a series of Python books and read about 1/4 of them. The rest is basically not moved yet. I just went back to dinner and watched movies at night. Recently I found that my head occasionally started to hurt. You are welcome to have a problem with the cervical spine. I am going to check it out this week. I hope you will pay more attention to your health, discover any discomfort and treat your health promptly. Well, it's far away. Today, I saw some information and Demos about Signalr, and I suddenly felt very useful. So I spent some time, referring to Boyou's materials, and I made one by myself, record it again, hoping to provide some help to friends who need it, and add maintenance materials to their knowledge base. There is something wrong with it.Criticism points out!

  Signalr Introduction

  ASP. NET SignalR is a class library under ASP. NET. It can implement real-time communication in ASP. NET Web projects. Real-time communication: Allows clients (Web pages) and servers to notify each other of messages and call methods. Of course, this is a real-time operation.

Use Signalr

   1. Create an mvc project. (Here I am using vs2015) I will not go into details about MVC here. The structure of the project, including pages and controllers, may be different from that of lower than 2015.

2. Select a project and add reference --- manage NuGet package packages. In the displayed dialog box, search for Signalr,

    

Click "Install". The installation instructions are as follows.

3. Code

A. First register the SignalR pipeline in app. MapSignalR (); in Startup.

 

Using Microsoft. owin; using Owin; [assembly: OwinStartupAttribute (typeof (SignalrPushInformation. startup)] namespace SignalrPushInformation {public partial class Startup {public void Configuration (IAppBuilder app) {ConfigureAuth (app); app. mapSignalR ();}}}View Code

 

B. Add the information processing class and inherit from the Hub. Here I will put it in the Models file. For personal reasons, I directly use a dictionary to store data.

 

Using Microsoft. aspNet. signalR; using Microsoft. aspNet. signalR. hubs; using System. collections; namespace SignalrPushInformation. models {[HubName ("PushMessage")] public class PushMessage: Hub {public void Init () {} public void Push (string title, string message) {this. insertMsg (title, message); // call the sendMessage method Clients of all Clients. all. sendMessage (title, message);} private void InsertMsg (string title, string message) {Message msg = new Message (); msg. title = title; msg. msgContent = message; MsgQueue. insert (msg );}}}View Code

Here we need to point out that the 'sendmessage' method is a custom method and will be used at the client. The parameters are consistent with the customer's requirements and the name is random.

C. Send Information view

@ {ViewBag. title = "SendMessage ";} <title> send messages </title> <script src = "/Scripts/jquery-1.10.2.min.js"> </script> <script src = "/Scripts/jquery. signalR-2.2.0.min.js "> </script> <script src ="/signalr/hubs "> </script> <script type =" text/javascript "> $ (function () {// reference the automatically generated hub proxy. The PushMessage here must be consistent with the information processing class name var chat =$. connection. pushMessage; // defines the client sendMessage called by the server to display new messages. The sendMessage method must be consistent in the information processing class, and the parameters must be consistent with the chat. Client. sendMessage = function (title, message) {// send the received message sendMsg () ;}; // The start of the integrator connection. $. connection. hub. start (). done (function () {// The service connection is complete. register the send button and click event $ ('# sendmessage '). click (function () {// call the Send method of the server hub. The push method must be consistent with the chat method in the information processing class. server. push ($ ("# title "). val (), $ ('# message '). val () ;}); sendMsg ();}). fail (function () {console. log ("cocould not Connect! ") ;}); Function sendMsg () {var options = {url: '/Message/MsgCount', type: 'post', success: function (data) {$ ("# count" 2.16.html (data. count) ;}}; $. ajax (options) ;}</script> D. view received information

@ {ViewBag. title = "ReceiveMessage ";} <title> Accept Messages </title> <script src = "/Scripts/jquery-1.10.2.min.js"> </script> <script src = "/Scripts/jquery. signalR-2.2.0.min.js "> </script> <script src ="/signalr/hubs "> </script> <script type =" text/javascript "> $ (function () {// reference the automatically generated hub proxy var chat =$. connection. pushMessage; // defines the client sendMessage called by the server to display new messages. The sendMessage method must be consistent in the information processing class, and the parameters must be consistent with chat. client. sendMessage = fun Ction (title, message) {// MsgCount (false) of the message sent to the page; $ ("# msgcontent "). after ("<div> title:" + title + ", message content:" + message + "</div>") ;}; // The Integrator connection starts $. connection. hub. start (). done (function () {MsgCount (true );}). fail (function () {console. log ("cocould not Connect! ") ;}); Function MsgCount (init) {var options = {url: '/Message/MsgCount', type: 'post', success: function (data) {if (data! = Null) {$ ("# count" cmd.html (data. count); console. log (data. msg); if (init) {$. each (data. msg, function (n, m) {$ ("# msgcontent "). append ("<div> title:" + m. title + ", message content:" + m. msgContent + "</div>") ;}}};$. ajax (options) ;}</script> F. Controller code

Using SignalrPushInformation. models; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; namespace SignalrPushInformation. controllers {public class MessageController: Controller {// GET: Message public ActionResult ReceiveMessage () {return View ();} public ActionResult SendMessage () {MsgQueue. insert (new Message () {MsgContent = "zcy", Title = "zcy"}); return View ();} [HttpPost] public JsonResult MsgCount () {return Json (new {count = MsgQueue. getCount (), msg = MsgQueue. dequeue ()});}}}View Code

Okay, everything is OK. You only need to compile and generate the code and execute it. The detailed explanations are all in the Code. This is not detailed here.

  StayLast

    I have published several articles from blog activation to the present, and I find that I am really too lazy. Sometimes I feel really bad at expressing and want to write something, but I don't know how to write it. In the future, I want to write a series of Python to deepen my understanding of Python. Although I have read a complete basic tutorial before, I found that there are still many things I don't understand. I hope that the formation of a blog can help me to deepen my understanding and get rid of my laziness ,, hope you can let the bloggers supervise me.

Old Rules, source code! (Password: MT4E)

  

 

   

   

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.