asp.net simple chat room program using application and Session Object-practical tips

Source: Internet
Author: User

Asp. NET has two important objects, one is the Application object, the other is the session object.

Application: An object that records application parameters that are used to share application-level information.

Session: Records a browser-side variable object that is used to store variables or objects across a Web-page program.

To tell you the truth, the Asp.net,application object that has been written for almost a year has not really been used. Read a book, according to the characteristics of these two objects wrote a simple chat room program. It's really very shabby.

My idea is that there are two page default pages and chatroom pages, page layout as shown:

Default page:

Chatroom page:

The idea is that, in the default page login, enter the nickname into the Session object, and jump to the chatroom page, and the session in the key to "nickname" into the Application object. Iterate over the Application object and add the nickname of the online person to the listbox on the left. Use the Appliaction object to +1,-1 the key to "count" in the Session_Start and Session_End methods to record the number of people currently online. The chat record is stored using the Application object key for "content".

The following code

Global.asax:

Copy Code code as follows:

protected void Application_Start (object sender, EventArgs e)
{
application["Count" = 0;
application["content"] = "chat record \ n";
}

protected void Session_Start (object sender, EventArgs e)
{
Application.Lock ();
application["Count" = (int) application["count" + 1;
Application.UnLock ();
}
protected void Session_End (object sender, EventArgs e)
{
Application.Lock ();
application["Count" = (int) application["Count"-1;
Application.UnLock ();
}

Default.aspx:

Copy Code code as follows:

protected void Button_login_click (object sender, EventArgs e)
{
if (!string. IsNullOrEmpty (Textbox_nickname. Text))
{
session["nickname"] = Textbox_nickname. Text;
}
Response.Redirect ("chatroom.aspx");
}

Chatroom.aspx:

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Label_count. Text = "Number of current online is" + application["Count"]. ToString () + "person";
if (! IsPostBack)
{
if (session["nickname"]!= null)
{
Application.add (session["nickname"). ToString (), session["nickname"]);
}
Textbox_records. Text = application["Content"]. ToString ();
}
Listbox_usernames. Items.clear ();
foreach (String str in application.contents)
{
if (!str. Equals ("content") &&!str. Equals ("Count") &&!str. Equals ("name"))
{
Listbox_usernames. Items.Add (New ListItem (APPLICATION[STR). ToString (), Application[str]. ToString ()));
}
}
}

        protected void Button_send_click (object sender, EventArgs e)
         {
            if ( session["nickname"]!= null)
            {
                 application["Content"] + + = ( session["nickname"]. ToString () + "said:" + textbox_sendmsg. Text + "\ n");
                textbox_records. Text = application["Content"]. ToString ();
           }
       }

protected void Button_exit_click (object sender, EventArgs e)
{
Application.remove (session["nickname"). ToString ());
application["Count" = (int) application["Count"-1;
Response.Redirect ("webcounter.aspx");
}

This simple chat room is complete, when used locally, in a browser does not seem to create a new session, you need to log in under different browsers to see the effect.

Also note here is that when exiting the chat room, the application key to the "nickname", that is, the current session["nickname" Value to remove. Otherwise, when you refresh the listbox, you will find that the exit nickname still exists.

Also want to put the key to "count" of the Application Object-1, perhaps you want to ask, not in the Session_End method has been-1? I'm not sure about this one, maybe there are two reasons, I don't know which one? Hope to understand the friend give advice.

1, the session has not expired, I just application the corresponding key for the session of the object to kill, but the real session I did not clear. Therefore, the Session_End method is not invoked. (but perhaps you will ask, do you put the current session["nickname"] Object empty not just OK? I tried this, but count didn't-1. )
2, will it be because I am in the local two different browsers to do this chat operation reason?

In some special circumstances, with the Application object to our help really very big, session I will not say, definitely is we often use.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.