<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dot Net Thoughts &#187; AJAX.Net</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/ajax-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A Simple Chat script using ASP.Net C#</title>
		<link>http://www.dotnetthoughts.net/2009/09/11/a-simple-chat-script-using-asp-net-c/</link>
		<comments>http://www.dotnetthoughts.net/2009/09/11/a-simple-chat-script-using-asp-net-c/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 12:50:34 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[AJAX.Net]]></category>
		<category><![CDATA[ASP.Net Chat script]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[Chat]]></category>
		<category><![CDATA[IIS 7]]></category>
		<category><![CDATA[PageMethods]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=373</guid>
		<description><![CDATA[Few days back one of my colleague was looking for a simple chat script in ASP.Net, but he can&#8217;t found one. Almost all of the chat scripts were in ASP and he want to do it for an internal site. So I thought about implementing one. And here is the simple script using ASP.Net&#8217;s Page [...]]]></description>
			<content:encoded><![CDATA[<p>Few days back one of my colleague was looking for a simple chat script in ASP.Net, but he can&#8217;t found one. Almost all of the chat scripts were in ASP and he want to do it for an internal site. So I thought about implementing one. And here is the simple script using ASP.Net&#8217;s Page method feature. For this I am using Windows Authentication, because it is for a intranet site, where users will be coming from one domain only.</p>
<p>Code behind &#8211; Global.asax</p>
<pre class="brush: csharp;">
private List&lt;string&gt; onlineUsers;
void Session_Start(object sender, EventArgs e)
{
	if (this.onlineUsers == null)
	{
            this.onlineUsers = new List&lt;/string&gt;&lt;string&gt;();
        }

        if (!this.onlineUsers.Contains(HttpContext.Current.User.Identity.Name))
        {
            this.onlineUsers.Add(HttpContext.Current.User.Identity.Name);
        }
        HttpContext.Current.Session[&quot;Users&quot;] = this.onlineUsers;
}

void Session_End(object sender, EventArgs e)
{
        if (this.onlineUsers != null)
        {
            this.onlineUsers.Remove(HttpContext.Current.User.Identity.Name);
        }
	HttpContext.Current.Session[&quot;Users&quot;] = this.onlineUsers;
}
</pre>
<p>Web.Config</p>
<pre class="brush: xml;">
&lt;authentication mode=&quot;Windows&quot;/&gt;
&lt;authorization&gt;
	&lt;deny users=&quot;?&quot;/&gt;
&lt;/authorization&gt;
</pre>
<p>Default.aspx.cs</p>
<pre class="brush: csharp;">
private static List&lt;/string&gt;&lt;string&gt; chatCollection;

[WebMethod(true)]
public static List&lt;/string&gt;&lt;string&gt; Add(string comment)
{
	string user = HttpContext.Current.User.Identity.Name;
        if (chatCollection == null)
        {
		chatCollection = new List&lt;/string&gt;&lt;string&gt;();
        }
        comment = comment.Replace(&quot;&lt; &quot;, &quot;[&quot;).Replace(&quot;&gt;&quot;, &quot;]&quot;);
        comment = GetSmileys(comment);
        chatCollection.Add(string.Format(&quot;&lt;tr&gt;&lt;td&gt;{0} :&lt;font color=\&quot;blue\&quot;&gt;{1}&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&quot;, user, comment));
        return chatCollection;
}

private static string GetSmileys(string comment)
{
        if (comment.Contains(&quot;[:-)]&quot;))
        {
        	comment = comment.Replace(&quot;[:-)]&quot;, &quot;&lt;img src=\&quot;icons\\smile.gif\&quot;/&gt;&quot;);
        }
        else if (comment.Contains(&quot;[;-)]&quot;))
        {
        	comment = comment.Replace(&quot;[;-)]&quot;, &quot;&lt;img src=\&quot;icons\\wink.gif\&quot;/&gt;&quot;);
        }
        else if (comment.Contains(&quot;[:-(]&quot;))
        {
        	comment = comment.Replace(&quot;[:-(]&quot;, &quot;&lt;img src=\&quot;icons\\frown.gif\&quot;/&gt;&quot;);
        }
        else if (comment.Contains(&quot;[:-D]&quot;))
        {
        	comment = comment.Replace(&quot;[:-D]&quot;, &quot;&lt;img src=\&quot;icons\\biggrin.gif\&quot;/&gt;&quot;);
        }
        else if (comment.Contains(&quot;[:-|]&quot;))
        {
        	comment = comment.Replace(&quot;[:-|]&quot;, &quot;&lt;img src=\&quot;icons\\blankstare.gif\&quot;/&gt;&quot;);
        }
        else if (comment.Contains(&quot;[B-)]&quot;))
        {
        	comment = comment.Replace(&quot;[B-)]&quot;, &quot;&lt;img src=\&quot;icons\\cool.gif\&quot;/&gt;&quot;);
        }
        else if (comment.Contains(&quot;[}-)]&quot;))
        {
        	comment = comment.Replace(&quot;[}-)]&quot;, &quot;&lt;img src=\&quot;icons\\devilish.gif\&quot;/&gt;&quot;);
        }
        else if (comment.Contains(&quot;[:-P]&quot;))
        {
        	comment = comment.Replace(&quot;[:-P]&quot;, &quot;&lt;img src=\&quot;icons\\p.gif\&quot;/&gt;&quot;);
        }
	return comment;
}

[WebMethod(true)]
public static List&lt;/string&gt;&lt;string&gt; GetChat()
{
	if (chatCollection == null)
        {
        	chatCollection = new List&lt;/string&gt;&lt;string&gt;();
        }
        return chatCollection;
}

[WebMethod(true)]
public static List&lt;/string&gt;&lt;string&gt; GetUsers()
{
	if (HttpContext.Current.Session[&quot;Users&quot;] == null)
        {
        	HttpContext.Current.Session[&quot;Users&quot;] = new List&lt;/string&gt;&lt;string&gt;();
        }
        return HttpContext.Current.Session[&quot;Users&quot;] as List&lt;/string&gt;&lt;string&gt;;
}
</pre>
<p>And finally the HTML</p>
<p>Default.aspx</p>
<pre class="brush: xml;">

&lt; !DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head runat=&quot;server&quot;&gt;
    &lt;title&gt;:: TEAM CHAT ::&lt;/title&gt;

    &lt;script type=&quot;text/javascript&quot;&gt;
        function sendChat() {
            var chatText = $get(&quot;txtChatInput&quot;).value;
            if (chatText != &quot;&quot;) {
                PageMethods.Add(chatText, sendChat_Callback);
            }
            else {
                $get(&quot;txtChatInput&quot;).focus();
            }
            return false;
        }
        function sendChat_Callback(response) {
            displayChatText(response);
            $get(&quot;txtChatInput&quot;).value = &quot;&quot;;
            $get(&quot;txtChatInput&quot;).focus();
        }
        function GetChat() {
            PageMethods.GetChat(GetChat_Callback);
        }
        function GetChat_Callback(response) {
            displayChatText(response);
        }
        function displayChatText(response) {
            var result;
            result = &quot;&lt;table&gt;&quot;;
            for (var i = response.length - 1; i &gt;= 0; i--) {
                result = result + response[i];
            }
            result = result + &quot;&lt;/table&gt;&quot;;
            $get(&quot;chatOutputDiv&quot;).innerHTML = result;
        }
        function getUsers() {
            PageMethods.GetUsers(GetUsers_Callback);
        }
        function GetUsers_Callback(response) {
            $get(&quot;lstUsers&quot;).options.length = 0;
            for (var i = response.length - 1; i &gt;= 0; i--) {
                addUsers(response[i]);
            }
        }
        function addUsers(text) {
            var optn = document.createElement(&quot;OPTION&quot;);
            optn.text = text;
            optn.value = text;
            $get(&quot;lstUsers&quot;).options.add(optn);
        }
        function LoadUI() {
            getUsers();
            GetChat();
            $get(&quot;txtChatInput&quot;).focus();
        }
        function showAbout() {
            var msg = &quot;Copyright (c) 2009 Anuraj. All rights reserved.\nLicenced under GNU GPL v2.&quot;;
            msg += &quot;\n\nVersion 0.2 : Supports Smileys\n&quot;;
            msg += &quot;Smileys should be in square brackets, like below,\ncase-sensitive(Sorry I will fix it later)\n&quot;;
            msg += &quot;Supported :- [:-)], [;-)], [:-(], [:-D], [:-|], [B-)], [}-)],[:-P]\n&quot;;
            msg += &quot;\n\nHappy Chatting [:)]\n&quot;;
            alert(msg);
        }
        setInterval(&quot;LoadUI();&quot;, 5000);
    &lt;/script&gt;

    &lt;style type=&quot;text/css&quot;&gt;
        body
        {
            font-family: Calibri;
            font-size: 11pt;
        }
        input
        {
            font-family: Calibri;
            font-size: 11pt;
            color: Blue;
        }
        .txt
        {
            border: solid 1px #000;
            font-family: Calibri;
            font-size: 11pt;
            color: Black;
        }
        option
        {
            font-family: Calibri;
            font-size: 11pt;
            color: Blue;
        }
        h1
        {
            color: Blue;
            font-size: xx-large;
            text-decoration: underline;
            text-align: center;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body onload=&quot;javascript:LoadUI();&quot;&gt;
    &lt;form id=&quot;form1&quot; runat=&quot;server&quot; onsubmit=&quot;javascript:return sendChat();&quot;&gt;
    &lt;div&gt;
        &lt;asp :ScriptManager runat=&quot;server&quot; ID=&quot;scriptMgr&quot; EnablePageMethods=&quot;true&quot; ScriptMode=&quot;Release&quot;
            EnableViewState=&quot;false&quot; /&gt;
        &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; border=&quot;0&quot;&gt;
            &lt;tr&gt;
                &lt;td colspan=&quot;2&quot; style=&quot;text-align: center&quot;&gt;
                    &lt;h1&gt;
                        :: TEAM CHAT ::
                    &lt;/h1&gt;
                    &lt;span&gt;A simple chat script using ASP.Net and C#&lt;/span&gt;
                    &amp;nbsp;&lt;a href=&quot;javascript:void('About');&quot; title=&quot;About TeamChat&quot; onclick=&quot;javascript:return showAbout

();&quot;&gt;?&lt;/a&gt;&amp;nbsp;
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;

                    &lt;input class=&quot;txt&quot; type=&quot;text&quot; maxlength=&quot;1500&quot; style=&quot;width: 340px;&quot; id=&quot;txtChatInput&quot; /&gt;
                &lt;/td&gt;
                &lt;td valign=&quot;middle&quot;&gt;
                    &amp;nbsp;&lt;input type=&quot;button&quot; id=&quot;cmdSubmit&quot; value=&quot;Send&quot; onclick=&quot;javascript:return sendChat();&quot; /&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td colspan=&quot;2&quot;&gt;
                    &amp;nbsp;
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td colspan=&quot;2&quot; valign=&quot;top&quot;&gt;
                    &lt;div id=&quot;chatOutput&quot; style=&quot;height: 300px; width: 400px; border: solid 1px #000;
                        overflow: scroll;&quot;&gt;
                        &lt;div id=&quot;chatOutputDiv&quot; style=&quot;height: 280px; width: 380px;&quot;&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td colspan=&quot;2&quot;&gt;
                    &amp;nbsp;
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td colspan=&quot;2&quot; valign=&quot;top&quot;&gt;
                    &lt;label&gt;
                        Online Users&lt;/label&gt;&lt;br /&gt;
                    &lt;select class=&quot;txt&quot; size=&quot;4&quot; id=&quot;lstUsers&quot; style=&quot;width: 400px; border: thin solid #000000;
                        font-family: Calibri; font-size: 11pt; color: #FFFFFF;&quot;&gt;
                        &lt;option&gt;No users available &lt;/option&gt;
                    &lt;/select&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td colspan=&quot;2&quot; style=&quot;text-align: center; font-size: 11px;&quot;&gt;
                    Copyright &amp;copy; 2009 &lt;a href=&quot;mailto:anuraj.p@mymailserver.com&quot;&gt;Anuraj&lt;/a&gt;. All rights
                    reserved.&lt;br /&gt;
                    Licenced under GNU GPL v2.
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Here is the Image &#8220;Team Chat&#8221; running on my machine</p>
<div id="attachment_374" class="wp-caption alignnone" style="width: 435px"><a href="http://anuraj.files.wordpress.com/2009/09/team_chat_image.jpg"><img src="http://anuraj.files.wordpress.com/2009/09/team_chat_image.jpg" alt="Team chat - asp.net script screenshot" title="Team Chat - Screen shot" width="425" height="580" class="size-full wp-image-374" /></a><p class="wp-caption-text">Team chat - asp.net script screenshot</p></div>
<p>Code seems like pretty self explantory. As wordpress don&#8217;t support zip attachments, I will upload, and give the source later.</p>
<p>Happy Chating <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I took the Emoticons from <a href="http://comments.deviantart.com/emoticons?order=popularity" target="_blank">deviantart.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/09/11/a-simple-chat-script-using-asp-net-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
