Click here to Skip to main content
14,149,579 members
Click here to Skip to main content
Add your own
alternative version

Stats

35K views
574 downloads
16 bookmarked
Posted 15 Apr 2010
Licenced CPOL

DWR like .NET Comet Ajax in ASP.NET

, 15 Apr 2010
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Oguz Bastemur
Software Developer (Senior) Pagos
Turkey Turkey
creating an application is a life style. not a job!

You may also be interested in...

Comments and Discussions

 
QuestionBind Method not present Pin
Prasanna Rajendra Newaskar30-Sep-15 1:44
professionalPrasanna Rajendra Newaskar30-Sep-15 1:44 
QuestionPokein .dll not Free !! Pin
Rocco Celenta20-Nov-12 0:00
memberRocco Celenta20-Nov-12 0:00 
GeneralGreat Work Pin
_skidrow_vn_5-Sep-10 17:43
member_skidrow_vn_5-Sep-10 17:43 
GeneralPokeIn Sources Pin
robottt2-Aug-10 21:00
memberrobottt2-Aug-10 21:00 
QuestionWhere I can find a VB.net sample? Pin
Member 332422820-Apr-10 4:30
memberMember 332422820-Apr-10 4:30 
AnswerRe: Where I can find a VB.net sample? Pin
Oguz Bastemur20-Apr-10 8:37
memberOguz Bastemur20-Apr-10 8:37 
GeneralRe: Where I can find a VB.net sample? Pin
Member 332422820-Apr-10 14:56
memberMember 332422820-Apr-10 14:56 
GeneralRe: Where I can find a VB.net sample? Pin
Oguz Bastemur20-Apr-10 16:09
memberOguz Bastemur20-Apr-10 16:09 
GeneralRe: Where I can find a VB.net sample? Pin
Member 332422821-Apr-10 4:43
memberMember 332422821-Apr-10 4:43 
GeneralRe: Where I can find a VB.net sample? Pin
Oguz Bastemur21-Apr-10 6:37
memberOguz Bastemur21-Apr-10 6:37 
GeneralRe: Where I can find a VB.net sample? Pin
Member 332422821-Apr-10 14:20
memberMember 332422821-Apr-10 14:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190518.1 | Last Updated 15 Apr 2010
Article Copyright 2010 by Oguz Bastemur
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid