Have not written code for several years, re-practice code, do notes in this memo.
aspx page JS code:
<script type= "Text/javascript" > Function showmsg () {//timed refresh get chat content var data = "{} "; $.ajax ({type: "POST", DataType: "JSON", ContentType: "Application/json; Charset=utf-8 ", url:" Webservice1.asmx/getmessagecontent ", success:function (Result) { var msgdiv = document.getElementById ("Messageshowdiv"); msgdiv.innerhtml = ""; msgdiv.innerhtml = RESULT.D; Keep the scroll bar at the bottom msgdiv.scrolltop = msgdiv.scrollheight; }})} var timer1 = Window.setinterval ("ShowMsg ()", 2000); Window.clearinterval (timer1) function showonlineusers () {var users = document.getElementById (" Onlineusersshow "); $.ajax ({type: "POST", DataType: "JSON", ContentType: "Application/jsoN;charset=utf-8 ", url:" Webservice1.asmx/getonlineusers ", success:function (Result) {users.innerhtml = ""; var str = result.d.substring (0,result.d.lastindexof ("|")). Split ("|"); var userstr = ""; $.each (str, function (I, Val) {userstr + = val + "<br/>"; }) users.innerhtml = Userstr; }})} var timer2 = Window.setinterval ("Showonlineusers ()", 10000); Window.clearinterval (timer2) function sendmsg () {var txt = document.getElementById ("Txtmessage "); if (Txt.value.length > 0) {var msgdata = "{Msg: '" + $ ("#txtMessage"). Val () + "'}"; $.ajax ({data:msgdata, type: "POST", DataType: "JSON", ContentType: "ApplicatioN/json; Charset=utf-8 ", url:" Webservice1.asmx/sendmsg ", success:function (Result) { if (Result.d = = "true") {showmsg (); Txt.value = ""; } else if (result.d = = "Outtime") {alert ("Login timeout, please login again!") "); window.location.href = ' _login.aspx '; } else {alert ("Communication sent Error:" + RESULT.D); }}, Error:function (e) {alert (E.D); } }); } else {alert ("Please enter content to send again ..."); }} </script>
ASMX code:
[WebMethod (Description = "Get chat history", EnableSession = True)] public string getmessagecontent () {if ( application["message"] = = null) {return ' no chat information currently. "; } int length = Convert.ToInt32 (session["messagelength"]); return application["Message"]. ToString (). Substring (length); } [WebMethod (Description = "Send chat content", EnableSession = True)] public string sendmsg (string Msg) { Verify that the user is logged in timeout if (session["UserName"] = = null) {return ' outtime '; } try {//user [TIME]: content String message = Msg.trim (); String username = session["username"]. ToString (); String datenow = DateTime.Now.ToLongTimeString (); String msg = Username + "[" + Datenow + "]:" + message + "<br/>"; Application.Lock (); application["Message"] = Application["Message"]. ToString () + msg; Application.UnLock (); Return "true"; } catch (Exception ex) {return ex. ToString (); }} [WebMethod (Description = "Get online user")] public string getonlineusers () {if (applic ation["onlinepersons"] = = null) {return ""; } return application["Onlinepersons"]. ToString (); }
Post-Login CS code:
protected void Btnlogin_click (object sender, EventArgs e) { //record user name session["username"] = username. Value; Increase Online user application.lock (); application["Online"] + = Username. Value + "|"; Application.UnLock (); Record the length of chat messages that exist at logon, only show chat messages that occur after login session["messagelength"] = 0; Application.Lock (); session["messagelength"] = application["Message"]. ToString (). Length; Application.UnLock (); Jump page Response.Redirect ("_showmessage.aspx"); }
Source Code Link: http://pan.baidu.com/s/1o7WuTGu
ASP. NET Practice ①--application Chat Room