ASP. NET use simple chat room program written by application and Session object

Source: Internet
Author: User

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:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/3E/2D/wKiom1PGS9aTpa0WAAAdDQlUfyg140.jpg "title=" QQ picture 20140716163832.jpg "alt=" Wkiom1pgs9atpa0waaaddqlufyg140.jpg "/>

Chatroom page:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/3E/2D/wKioL1PGTPqiNxxIAAClnxTP2Lw380.jpg "title=" QQ picture 20140716170239.jpg "alt=" Wkiol1pgtpqinxxiaaclnxtp2lw380.jpg "/>

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:

        protected void application_start (Object sender,  eventargs e)         {             application["Count"] = 0;             application["Content"] =  "chat history \ n";         }        protected void session_start ( Object sender, eventargs e)         {             application.lock ();             application["Count"] =  (int) application["Count"] + 1;             application.unlock ();       &nbSp; }        protected void session_end (object  Sender, eventargs e)         {             application.lock ();             application["Count"] =  (int) application["Count"] - 1;             application.unlock ();         }

Default.aspx:

        protected void  Button_login_click (object sender, eventargs e)         {             //application.add ("nickname"  +  application["Count"]. ToString (),  textbox_nickname. Text);            if  (!string. IsNullOrEmpty (Textbox_nickname. Text))             {                 session["nickname"] = textbox_ Nickname. text;            }             response.redirect ("chatroom.aspx");         } 

Chatroom.aspx:

        protected void page_load (object sender,  Eventargs e)         {             label_count. text =  "The current number of online people is  + application[" count "]. ToString ()  +  "People";            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");        &nbsP;        textbox_records. text = application["Content"]. ToString ();            }         }        protected void button_exit_ Click (object sender, eventargs e)         {             application.remove (Session["nickname"). ToString ());             //session["nickname"]  = null;            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.


Sina Weibo:http://weibo.com/zhouhongyu1989 welcome Onlookers ~!

This article is from the "Give Me a Gener cigarette" blog, please be sure to keep this source http://zhouhongyu1989.blog.51cto.com/2931598/1439105

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.