Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

DWR like .NET Comet Ajax in ASP.NET

0.00/5 (No votes)
15 Apr 2010 1  
Easy DWR like Comet in ASP.NET

PokeIn Comet Ajax for .NET

Think about a library that you can easily access to server side objects from the client side or vice versa? I will explain how it's easy do this with PokeIn.

First, PokeIn is a free library and you can download it from here!

Let's create a simple application step by step. I will mention about the "Bind" method to find our way.

public static bool Bind(string listenUrl, string sendUrl,
    System.Web.UI.Page page, DefineClassObjects classDefs, out string ClientId);

"Bind" provides the definition of server side objects on a client side. Also, with this method, the server side knows that there is a client connection request. "Bind" defines an unique ClientId for a client, creates the specified user classes and defines them on the client side. Shortly, if you add "Bind" method to CometAjax needed page, it will organize your page to communicate server side, i.e.:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
   Inherits="MyPage._Default" %>
<html>
<head>
    <%
        string ClientId = ""; //<- it stands to use ClientId on Default.Aspx page, 
           i.e., you may define server side events for client side objects via PokeIn ;)
        if (!PokeIn.Comet.CometWorker.Bind("Listen.aspx", "Send.aspx",
            this, new PokeIn.Comet.DefineClassObjects(MyPage._Default.classDefiner),
            out ClientId))
        {
            Response.Write("Application Stopped!");
            return;
        }
    %>
</head>

"Bind" method, defines PokeIn JavaScript methods and given server side objects. What about "MyPage._Default.classDefiner" ? It will create the objects that you defined per each client. So, every single object knows that the client belonged.

The code below is automatically generated by Visual Studio except the "classDefiner" method.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public static void classDefiner(string clientId, ref Dictionary<string> classList)
    {
        // HERE we define our Dummy class per each client
        classList.Add("Dummy", new Dummy(clientId)); 
    }
}

We defined our "Dummy" class associated with the clientId. Let me show Dummy class.

public class Dummy
{
    string ClientId;
    public Dummy(string clientId)
    {
        ClientId = clientId;
    }
    public void Calculate(int x, int y)
    {
        PokeIn.Comet.CometWorker.SendToClient(ClientId,
            "ResultCalculated(" + (x + y).ToString() + ");"); 
    }
}

If your class needs the associated client information, you have a chance to hold it as I show in the example above. You will use static PokeIn methods with this unique client id, i.e., in the above example, we called "SendToClient" method. It runs the JavaScript code on the owner of clientId.

Here is the client side use of Dummy class:

Dummy.Calculate(3, 4);

Isn't that very easy? So, we sent "ResultCalculated" from our server side method for "Dummy.Calculate" call. Thus, client side will need this method definition. As you know, we called it "ResultCalculated" but don't forget that you can send whatever you want.

The definition of "ResultCalculated" is:

function ResultCalculated( result )
{
    alert( result );
}

Before we start our application, there are 2 definitions left. "Listen.aspx" and "Send.aspx" parameters in "Bind" method? Names do not matter. Just add the lines below to define 2 different way handlers, i.e., Listen.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Listen.aspx.cs" 
    Inherits="MyPage.Listen" %>
<% 
    //Check UserLogin etc
    PokeIn.Comet.CometWorker.Listen(this);
%>

and Send.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Send.aspx.cs" 
    Inherits="MyPage.Send" %>
<% 
    //Check UserLogin etc
    PokeIn.Comet.CometWorker.Send(this);
%>

PokeIn uses the "Listen" definition for server push operations and the "Send" definition for client push operations.

End!

Our comet Ajax based web application is completed! It's easier than DWR!

Using the Code

Download the code attached to this article to see how it works. You should download the library from here!

PokeInSample

  1. Open the project.
  2. Right click to Project References.
  3. Add the "PokeIn.dll" you downloaded.

Run!

History

  • 15th April, 2010: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here