Asp.net exercise ① -- Application chat room, asp. netapplication
I have not written the code for several years, and I have re-practiced the code and made notes here.
Js Code of the aspx page:
<Script type = "text/javascript"> function showMsg () {// regularly refresh to obtain 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 of msgdiv. scrollTop = msgdiv. scrollHeigh T ;}})} 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. las TIndexOf ("| ")). 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 ("Logon timeout. Please log on again! "); Window. location. href = '_ login. aspx ';} else {alert ("message sending error:" + result. d) ;}}, error: function (e) {alert (e. d) ;}}) ;}else {alert ("Enter the content before sending... ") ;}}</script>
Asmx code:
[WebMethod (Description = "get chat record", 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 whether the user has timed out. 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 (Application ["OnlinePersons"] = null) {return "";} return Application ["OnlinePersons"]. toString ();}
Cs code after Logon:
Protected void btnLogin_Click (object sender, EventArgs e) {// record the user name Session ["username"] = username. value; // adds an online user Application. lock (); Application ["online"] + = username. value + "|"; Application. unLock (); // record the length of the chat information that exists during logon. Only the chat information Session ["messageLength"] = 0; Application. lock (); Session ["messageLength"] = Application ["Message"]. toString (). length; Application. unLock (); // jump to the page Response. redirect ("_ showMessage. aspx ");}
Source code: http://pan.baidu.com/s/1o7WuTGu