Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Java

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

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
22 Jan 2013CPOL5 min read 55.1K   1.4K   42  
An article about how to keep AJAX simple as it is and get the most out of it.
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)


Written By
Software Developer
New Zealand New Zealand
Coder

Comments and Discussions