Home > Uncategorized > ASP.NET chatroom with out of band calls.

ASP.NET chatroom with out of band calls.

Chatrooms are typically developed in Flash or java, since they both contain the facility to make HTTP requests to the server from which the Applet / SWF was downloaded from. An ActiveX control named Microsoft.XMLHTTP also has the same capability, and is capable of running on browsers with default security settings (I believe it may be marked ‘safe for scripting’). I also believe that it is compatible with FireFox, and IE7 are relaxing security for this specific control.

So, I decided to put together a simple chatroom example in asp.net using this technology. Based on an excellent example by Dino Esposito.

Starting off with a simple html form thus:

<form runat="server">
   <h1>Demonstrate Simple chatroom with Out-of-band Calls</h1>
   <hr>
   Message: <input type="text" name="txtMessage">
   <Button Runat="server" ID="ButtonGo">Send & Receive</Button>
   <hr>
   <span ID="Msg" />
  </form>

and some associated javascript:

<SCRIPT language="javascript">
    setTimeout("Callback(”)",2000);
 function DoCallback(url, params)
    {
  var pageUrl = url + "?callback=true&param=" + params;
  var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
  xmlRequest.open("POST", pageUrl, false);
        xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlRequest.send(null);
        return xmlRequest;
    }
   
    function Callback(message)
    {
  var xmlRequest = DoCallback("callback.aspx", message);  
  Msg.innerHTML = xmlRequest.responseText;
     if (message==”) setTimeout("Callback(”)",2000);
    }
</SCRIPT>

As we can see, a post request is sent back to callback.aspx, which handles the request thus:

<script runat="server">
private void Page_Load(object sender, EventArgs e)
{
 if (Request.QueryString["callback"] != null)
 {
  string param = Request.QueryString["param"].ToString();
  Response.Write(RaiseCallbackEvent(param));
  Response.Flush();
  Response.End();  
 }
 else
 {
  string callbackRef = "Callback(document.all[‘txtMessage’].value)";
  ButtonGo.Attributes["onclick"] = callbackRef;
 }
}

 string RaiseCallbackEvent(string eventArgument)
 {
  if (eventArgument!="")
  {
   Application["conversation"] += eventArgument + "<br>";
  }
  return Application["conversation"].ToString();
 }
 
</script>

Pretty simple. To check out a demo see www.globefinder.info/callback.aspx

 

 

 

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: