Implementing code to implement message notification mechanism using ASP.net MVC 4 Async action+jquery Practical Tips

Source: Internet
Author: User

These two days, using the ASP.net MVC 4 Development Comet Message Notification mechanism, the backend uses an asynchronous thread to subscribe to the message, and the client requests an action in MVC through an Ajax long connection, such as: Http://localhost/event/imageSet, Gets the change message for the ImageSet object (new, update, and delete messages).

1. Definition of Class Ievententity<tentity> class for event messages

Copy Code code as follows:

public interface ientityevent<tentity>
{
Entity class object for change
Tentity[] Entities
{
Get
}

Type of action
Entityeventtype Type
{
Get
}
}

public enum Entityeventtype:int
{
Create = 0,
Update = 1,
Removed = 2
}


2.EntityEventController class

Copy Code code as follows:

[SessionState (Sessionstatebehavior.readonly)]
public class Entityeventcontroller:controller
{
The Change event action,millsecondstimeout to the ImageSet object operation is timed out asynchronously.
Public async task<actionresult> ImageSet (int millisecondstimeout = 10000)
{
return await this. Eventasync<imagesetdata> (millisecondstimeout);
}

Private async task<actionresult> eventasync<tentity> (int millisecondstimeout)
{
Ientityevent<tentity> entityevent = await entityeventsubcriber.instance.waitforentityevent<tentity> ( millisecondstimeout);

return this. Json (New
{
Hasevent = null!= entityevent,
Entityevent = Entityevent

}, Jsonrequestbehavior.allowget);
}
}


(1) This is the technique used in the. Net Framework 4.5 to make the current asynchronous ASP.net MVC async Action (Reference: Using asynchronous Methods in asp.net MVC 4 technical article), where the Before method a The Sync keyword can be used in conjunction with a Task object, indicating that the method is an asynchronous method that generates the logical code of the associated asynchronous operation required by the runtime, and that the method must use the await statement to wait for the end of an asynchronous operation, await and task<t> Combine to return result of task completion

(2) If the controller layer application or the filter in the operation of the session, so in order to avoid long links will not lead to the same session in other calls in the case of block, you need to add on the controller header [ sessionstate (sessionstatebehavior.readonly)] to indicate that the current controller session is read-only, so that other operations will not be blocked.

3. The code for the Entityeventsubscriber message Subscriber is not specifically written here, and will be described in detail in the article "Message subscriptions and publications".

4.jQuery AJAX Client Code

Copy Code code as follows:

$ (document). Ready (function () {

var $hoverList = $ ("#imageSets"). Hoverlist ({title: "Picture Set list", selectedindex:1});

var getEvent = function () {

var getpattern = "/easyshirtbackend/imageset/0";

$.getjson ("/easyshirtbackend/event/imageset/100000", function (data) {

if (data. Hasevent) {

Create
if (data. Entityevent.type = = 0) {

$.each (data. Entityevent.entities, function (I, entity) {

TODO: Handling Entity classes new

if (i = = data. ENTITYEVENT.ENTITIES.LENGTH-1) {

GetEvent ();
}
});

Return
}

Update
if (data. Entityevent.type = = 1) {

$.each (data. Entityevent.entities, function (I, entity) {

TODO: Handling Entity class updates

if (i = = data. ENTITYEVENT.ENTITIES.LENGTH-1) {

GetEvent ();
}
});

Return
}


Delete
if (data. Entityevent.type = = 2) {

$.each (data. Entityevent.entities, function (I, entity) {

TODO: Handling entity class deletes

if (i = = data. ENTITYEVENT.ENTITIES.LENGTH-1) {

GetEvent ();
}
});
}

}else{

$ ("#imageSets"). Hoverlist ("Add", data);
GetEvent ();
}
});
};

GetEvent ();
});


The main thing to control in code is to get a message (whether it's a message or no message), you need to call the GetEvent () method at the right time to get a message loop.

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.