Ok, so I am new to Async controllers, and the COMET concept. But I have successfully created a COMET implementation using MVC 2 RTM and jQuery.
I am working on a Web application for organizing a physical filing system (for a large group of file cabinets in an office). I need a synchronization subsystem which will keep edited content synchronized among the many clients.
The following code WORKS. When I edit a "file" record using the service, the content from the response of the Async controller action is then sent to a custom alert subsystem. I am now wiring up the content sync subsystem so that when a file is edited, the
contents are pushed out to the other clients.
Question: Am I doing this right? Am I using the .net framework and threading appropriately? Any suggestions?
Here is the Async Controller:
public class SyncController : AsyncController
{
private FileSync sync;
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
sync = new FileSync();
base.Initialize(requestContext);
}
public void IndexAsync()
{
sync.Listen();
}
public ActionResult IndexCompleted()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (File item in FileSync.UpdateList)
{
sb.AppendFormat("Updated File: {0}", item.fileTitle);
}
//Simple return to alert the user of the titles of the files recently changed.
return Content(sb.ToString());
}
}
Here are the events and other classes:
public delegate void FileUpdatedHandler();
public class ListWithEvent : ArrayList
{
public event FileUpdatedHandler Updated;
protected virtual void OnUpdated()
{
if (Updated != null)
{
Updated();
}
}
public override int Add(object value)
{
int ret = base.Add(value);
OnUpdated();
return ret;
}
}
public class FileSync
{
public static ListWithEvent UpdateList;
private EventWaitHandle wait;
public FileSync()
{
if (UpdateList == null)
UpdateList = new ListWithEvent();
}
public void AddUpdate(File file)
{
UpdateList.Add(file);
}
public void ClearUpdates()
{
UpdateList.Clear();
}
public void Listen()
{
wait = new EventWaitHandle(false, EventResetMode.AutoReset);
FileUpdatedHandler handler = new FileUpdatedHandler(UpdateList_Updated);
UpdateList.Updated += handler;
wait.WaitOne();
wait.Close();
UpdateList.Updated -= handler;
}
private void UpdateList_Updated()
{
wait.Set();
}
}
here is the usage for an updated file:
public void UpdateFile(File file, string UserName)
{
}
here is the jQuery ajax function in use:
function SyncInit() {
$.ajax({
url: '/FileMaster/Sync/Index',
type: 'post',
beforeSend: function (oXhr) { oXhr.setRequestHeader('Connection', 'Keep-Alive'); },
dateType: 'html',
success: function (data) { window.parent.$$.Alert.SetAndOpen(data); SyncInit(); },
cache: false,
async: true
});
}
jimslattery
Member
4 Points
4 Posts
MVC 2 Async Controller COMET concept
May 01, 2010 05:14 PM|LINK
Ok, so I am new to Async controllers, and the COMET concept. But I have successfully created a COMET implementation using MVC 2 RTM and jQuery.
I am working on a Web application for organizing a physical filing system (for a large group of file cabinets in an office). I need a synchronization subsystem which will keep edited content synchronized among the many clients.
The following code WORKS. When I edit a "file" record using the service, the content from the response of the Async controller action is then sent to a custom alert subsystem. I am now wiring up the content sync subsystem so that when a file is edited, the contents are pushed out to the other clients.
Question: Am I doing this right? Am I using the .net framework and threading appropriately? Any suggestions?
Here is the Async Controller:
public class SyncController : AsyncController
{
private FileSync sync;
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
sync = new FileSync();
base.Initialize(requestContext);
}
public void IndexAsync()
{
sync.Listen();
}
public ActionResult IndexCompleted()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (File item in FileSync.UpdateList)
{
sb.AppendFormat("Updated File: {0}", item.fileTitle);
}
//Simple return to alert the user of the titles of the files recently changed.
return Content(sb.ToString());
}
}
Here are the events and other classes:
public delegate void FileUpdatedHandler(); public class ListWithEvent : ArrayList { public event FileUpdatedHandler Updated; protected virtual void OnUpdated() { if (Updated != null) { Updated(); } } public override int Add(object value) { int ret = base.Add(value); OnUpdated(); return ret; } } public class FileSync { public static ListWithEvent UpdateList; private EventWaitHandle wait; public FileSync() { if (UpdateList == null) UpdateList = new ListWithEvent(); } public void AddUpdate(File file) { UpdateList.Add(file); } public void ClearUpdates() { UpdateList.Clear(); } public void Listen() { wait = new EventWaitHandle(false, EventResetMode.AutoReset); FileUpdatedHandler handler = new FileUpdatedHandler(UpdateList_Updated); UpdateList.Updated += handler; wait.WaitOne(); wait.Close(); UpdateList.Updated -= handler; } private void UpdateList_Updated() { wait.Set(); } }here is the usage for an updated file:
public void UpdateFile(File file, string UserName) {