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 = ""; 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)
{
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" %>
<%
PokeIn.Comet.CometWorker.Listen(this);
%>
and Send.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Send.aspx.cs"
Inherits="MyPage.Send" %>
<%
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!
- Open the project.
- Right click to Project References.
- Add the "PokeIn.dll" you downloaded.
Run!
History
- 15th April, 2010: Initial post