Application session enables simple statistics on the number of online chats

Source: Internet
Author: User

Asp.net,application, who wrote for almost a year, didn't really use it. Read a book, according to the characteristics of these two objects wrote a simple chat room program. It's really very shabby.

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

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

Session: A variable object that records the browser side and is used to store variables or objects that are cross-Web program programs.

To tell the truth, the Asp.net,application object that has been written for almost a year is really not 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 pages of the default page and chatroom page, page layout

Default page:

Chatroom page:



The idea is that in the default page login, the nickname entered into the Session object, and jump to the chatroom page, and the session in the key to "nickname" stored in the Application object. Iterate through 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 current number of people online. The chat record is stored with the Application object key for "content".

The code below

Global.asax:

Copy CodeThe code is as follows:
protected void Application_Start (object sender, EventArgs e)
{
application["Count"] = 0;
application["Content" = "chat log \ 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 CodeThe code is 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 CodeThe code is as follows:
protected void Page_Load (object sender, EventArgs e)
{
Label_count. Text = "Current online number 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 () + "say:" + 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 finished, when used locally, in a browser does not seem to create a new session, you need to login in different browsers to see the effect.

It is also important to note that when exiting the chat room, the application key is "nickname", that is, the value of the current session["nickname") is removed. Otherwise, when you refresh the listbox, you will find that the nickname for the exit is still present.

Also the key to "count" of the Application Object-1, perhaps you have to ask, is not in the Session_End method already-1? I'm really not sure, maybe there are two reasons, do not know which one? The friend who wants to understand gives advice.

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

In some special cases, using the Application object to our help is really very large, the session I have to say, definitely we often use.

Application session enables simple statistics on the number of online chats

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.