Click here to Skip to main content
Licence CPOL
First Posted 15 Apr 2010
Views 14,300
Downloads 321
Bookmarked 16 times

DWR like .NET Comet Ajax in ASP.NET

By | 15 Apr 2010 | Article
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)

About the Author

Oguz Bastemur

Software Developer (Senior)
Pagos
Turkey Turkey

Member

creating an application is a life style. not a job!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGreat Work Pinmember_skidrow_vn_17:43 5 Sep '10  
GeneralPokeIn Sources Pinmemberrobottt21:00 2 Aug '10  
QuestionWhere I can find a VB.net sample? PinmemberMember 33242284:30 20 Apr '10  
AnswerRe: Where I can find a VB.net sample? PinmemberJashuaNet8:37 20 Apr '10  
GeneralRe: Where I can find a VB.net sample? PinmemberMember 332422814:56 20 Apr '10  
GeneralRe: Where I can find a VB.net sample? PinmemberJashuaNet16:09 20 Apr '10  
GeneralRe: Where I can find a VB.net sample? PinmemberMember 33242284:43 21 Apr '10  
GeneralRe: Where I can find a VB.net sample? PinmemberJashuaNet6:37 21 Apr '10  
GeneralRe: Where I can find a VB.net sample? PinmemberMember 332422814:20 21 Apr '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Apr 2010
Article Copyright 2010 by Oguz Bastemur
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid