Click here to Skip to main content
11,584,040 members (39,898 online)
Click here to Skip to main content
Articles » Web Development » Ajax » General » Downloads
Add your own
alternative version

Ajaxion - Standalone AJAX - Part 2 of 2 - C# and Java Example

, 22 Jan 2013 CPOL 36.4K 1.2K 42
An article about how to keep AJAX simple as it is and get the most out of it.
ajaxiontest1.0_java_src.zip
AjaxionTest
ajaxionTest.jpr
ajaxionTest.jpr.bak
AjaxionTest.jws
classes
ajaxionTest.cdi
pw
ajaxion
AjaxionEventConsumer.class
log
FileLog.class
servlet
ajaxionTest.class
servlet.cdi
public_html
css
images
processing.gif
ready.jpg
js
WEB-INF
pw.ajaxion
classes
pw
ajaxion
AjaxionEventConsumer.class
AjaxionEventHandler.class
pw.ajaxion.cdi
pw.ajaxion.jpr
src
pw
ajaxion
pw.log
classes
audit.log
pw
log
FileLog.class
pw.log.cdi
test
pw
log
FileLogTest.class
pw.log.jpr
src
pw
log
test
pw
log
src
servlet
ajaxiontest1.0_net1.1_src.zip
Ajaxion
Ajaxion.csproj.user
Ajaxion.ico
js
AjaxionTest.csproj.webinfo
Audit
Audit.csproj.user
Audit.ico
css
Global.asax
images
AjaxFlavour1.gif
AjaxFlavour2.gif
AjaxFlavour3.jpg
App.ico
HappyAccountant.jpg
processing.gif
ready.JPG
js
ajaxiontest1.0_net2.0_src.zip
Ajaxion.csproj.user
Ajaxion.ico
App_Code
Audit.csproj.user
Audit.ico
Global.asax
AjaxFlavour1.gif
AjaxFlavour2.gif
AjaxFlavour3.jpg
App.ico
HappyAccountant.jpg
processing.gif
ready.JPG
using System;
using System.Threading;
using System.Collections;
using System.Web;
using System.Web.UI;

namespace Ajaxion
{
	/// <summary>
	/// AjaxionEventConsumer.
	/// </summary>
	/// <example>
	/// AjaxionEventConsumer consumer = new AjaxionEventConsumer(this.Context, "imageGifUrl", "image/GIF");
	/// consumer.ConsumeEvent("images/" + consumer.EventParameters); 
	///</example>
	public class AjaxionEventConsumer
	{
		private const string URL_EVENT = "e";
		private const string URL_PARAM = "p";
		//private const string URL_DATE = "d";

		protected HttpContext contextHttp;

		protected string eventId;
		protected string parameters;

		public string EventId
		{
			get
			{
				return eventId;
			}
		}
		public string Parameters
		{
			get
			{
				return parameters;
			}
		}

		public HttpRequest Request
		{
			get
			{
				return contextHttp.Request;
			}
		}
		public HttpResponse Response
		{
			get
			{
				return contextHttp.Response;
			}
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="contextHttp">Caller HttpContext</param>
		/// <param name="eventId">Allowed callback events, as pairs of event id / content type</param>
		public AjaxionEventConsumer(HttpContext contextHttp)
			: this(contextHttp, 0)
		{}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="contextHttp">Caller HttpContext</param>
		/// <param name="eventId">Allowed callback events, as pairs of event id / content type</param>
		/// <param name="sleep">Miliseconds to sleep before consuming calls
		///  - mostly for demo, we do not want delays!</param>
		public AjaxionEventConsumer(HttpContext contextHttp, int sleep)
		{
			this.contextHttp = contextHttp;
			eventId = contextHttp.Request.QueryString[URL_EVENT];

			parameters = contextHttp.Request.QueryString[URL_PARAM];
			if (parameters == null)
				parameters = string.Empty;

			if (sleep > 0)
				Thread.Sleep(sleep);
		}

		public void ConsumeEvent(string responseData, string responseContentType)
		{
			BeginResponse(responseContentType);
			contextHttp.Response.Write(responseData);
			EndResponse();
		}
		public void ConsumeEvent(byte[] responseData, string responseContentType)
		{
			BeginResponse(responseContentType);
			contextHttp.Response.Write(responseData);
			EndResponse();
		}

		public void BeginResponse(string contentType)
		{
			contextHttp.Response.ContentType = contentType;
			contextHttp.Response.Flush();
		}
		public void RespondString(string responseData)
		{
			if (contextHttp.Response.ContentType.IndexOf("text") > -1)
			{
				contextHttp.Response.Write(responseData);
				contextHttp.Response.Flush();
			}
			else
				throw new HttpException("Unexpected content type...");
		}
		public void RespondBytes(byte[] responseData, string contentType)
		{
			if (contextHttp.Response.ContentType.IndexOf("text") == -1)
			{
				contextHttp.Response.Write(responseData);
				contextHttp.Response.Flush();
			}
			else
				throw new HttpException("Unexpected content type...");
		}
		public void EndResponse()
		{
			contextHttp.Response.Flush();
			contextHttp.Response.End();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

radumi
Software Developer
New Zealand New Zealand
Coder

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 22 Jan 2013
Article Copyright 2008 by radumi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid