Asp.net Instant Messaging (Ajax)

Source: Internet
Author: User

The following is a recent applicationAsp.netThe implementation of the small instant messaging function, because of the instant messaging, and the network is stateless. Therefore, I was unable to think of a good solution for a time. Many people have said that it can be used.SocketProgramming to Implement point-to-point instant communication using ports.

 

However, no examples of ready-made implementations are currently available. As a result, I still use a method like implementing a simple chat room.ApplicationObjects, global maintenance of a user message table to realize the transfer of information from one client to the server, and then from the server to another client (this is also the key to the problem, I can not achieve through the server, to achieve point-to-point message transmission, even if the server does not passApplicationThis sharing method is used to transfer information ).

 

The main idea and implementation method are as follows: the sender: Check whether the client is online -->Send messages (written to the database); Acceptor:

Receive message -- > View message -- > Reply (write to database) Or Ignore -- > If there is one, repeat the preceding steps. The most important part is how the receiving end receives messages, and whether the receiving end stays on any page, we must have a way to remind users of new messages (in fact, this is OA System features, that is, the user may be doing other things at the time, but there is a way to let him receive a message ). How to receive messages is actually a very simple method, that is, using Timer Control. Application The Round-Robin listener in the object (which is similar to the function of regularly refreshing) and whether new messages are waiting for receiving. If you want to implement new messages on every page, you will be prompted. Frame The idea is very simple. You must prompt the user on a global framework page, and listen for messages must also be embedded in this Frame .

 

Okay. The following is an example.Code:

 

 

 

First jumpMainform. aspxPage (composed of the following frameworks ):

 

<Textarea Cols = "85" rows = "15" name = "code" class = "XHTML"> <frameset rows = "129, * "Cols =" * "frameborder =" no "border =" 0 "framespacing =" 0 "> <br/> <frame src =" Top. aspx "mce_src =" Top. aspx "name =" topframe "scrolling =" no "noresize> <br/> <frameset Cols =" 250, * "frameborder =" no "> <br/> <frame src =" left. aspx "mce_src =" left. aspx "name =" leftframe "scrolling =" yes "noresize> <br/> <frameset rows =" 30, * "frameborder =" no "id =" right "> <br/> <frame scrolling =" no "name =" right_topframe "src =" showmsg. aspx "mce_src =" showmsg. aspx "frameborder =" 0 "> <br/> <frame src =" choose. aspx "mce_src =" choose. aspx "name =" rightframe "noresize scrolling =" yes "> <br/> </frameset> <br /> <body> <br/> <noframes> <br/> </body> <br/> </textarea>

 

Where,NameIsRight_topframeOfFrameIt is the framework page used to listen for messages, and all other pages are inNameIsRightframeOfFrame.

 

InApplicationThe following user object array is maintained in the object, whereUsers. CSAs follows:

 

Public class users <br/>{< br/> private string userid; <br/> private string username; <br/> private bool isonline; <br/> private stringbuilder message_text; <br/> private datetime message_time; <br/> private string message_to; <br/> private string message_from; <br/> private queue message_queue; </P> <p> public users (string UID) <br/>{< br/> This. userid = uid; <br/> This. isonline = false; <br/> message_text = new stringbuilder (""); <br/> message_to = userid; <br/> message_from = userid; <br/> // set a previous time <br/> message_time = convert. todatetime ("00:00:00"); <br/> message_queue = new Queue (); <br/> message = new messageinfo (message_text.tostring (), message_time, message_from ); <br/>}</P> <p> // account <br/> Public String userid <br/>{< br/> get {return userid ;} <br/> set {userid = value ;} <br/>}</P> <p> // name <br/> Public String username <br/>{< br/> get {return username ;} <br/> set {username = value ;} <br/>}</P> <p> // store the message <br/> Public stringbuilder message_text <br/>{< br/> get {return message_text ;} <br/> set {message_text = value ;} <br/>}</P> <p> // online <br/> Public bool isonline <br/>{< br/> get {return isonline ;} <br/> set {isonline = value ;} <br/>}</P> <p> // information recipient <br/> Public String message_to <br/>{< br/> get {return message_to ;} <br/> set {message_to = value ;} <br/>}</P> <p> // message sender <br/> Public String message_from <br/>{< br/> get {return message_from ;} <br/> set {message_from = value ;} <br/>}</P> <p> // time when information is received <br/> Public datetime message_time <br/>{< br/> get {return message_time ;} <br/> set {message_time = value ;} <br/>}</P> <p> // message queue used to receive messages <br/> Public queue message_queue <br/>{< br/> get {return message_queue ;} <br/> set {message_queue = value ;} <br/>}</P> <p> // used to store message-Related Information <br/> public struct messageinfo <br/>{< br/> Public String message_text; <br/> Public datetime message_time; <br/> Public String message_from; </P> <p> Public messageinfo (string m_text, datetime m_time, string m_from) <br/>{< br/> message_text = m_text; <br/> message_time = m_time; <br/> message_from = m_from; <br/>}</P> <p> // <summary> <br/> // defines an object of the message struct type. <br/ >/// </Summary> <br/> Public messageinfo message; <br/>}< br/>

 

To facilitate access, we first obtainID, Name, and other information, inUsersIn the arrayUserObject initialization, in order to facilitate access in the form of key-value pairs, we firstUsersEach element in the array is stored inIDAsKeyOfHashtable.ProgramIt must be completed at startup, and the user message table must be maintained during the entire application running.Global. asaxMediumApplication_startThe method is as follows:

Protected void application_start (Object sender, eventargs e) <br/>{< br/> users_ds DS = new users_ds (); <br/> users_dstableadapters.userstableadapter userada = new IM. users_dstableadapters.userstableadapter (); <br/> userada. fillall (Ds. users); <br/> int COUNT = Ds. users. rows. count; <br/> common. users [] users = new common. users [count]; <br/> hashtable ht_users = new hashtable (); <br/> for (INT I = 0; I <count; I ++) <br/> {<br/> Users [I] = new common. users (Ds. users. rows [I] [0]. tostring (); <br/> Users [I]. username = Ds. users. rows [I] [1]. tostring (); <br/> ht_users [users [I]. userid] = users [I]; <br/>}< br/> If (application ["users"] = NULL) <br/>{< br/> application ["users"] = ht_users; <br/>}< br/>

 

 

 

This is the general principle.

 

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.