asp.net練習①——Application聊天室

來源:互聯網
上載者:User

標籤:ring   href   win   html   sage   ajax   innerhtml   out   charset   

已經好幾年沒寫過代碼,重新練習起代碼,在這做做筆記備忘。

aspx頁面js代碼:

<script type="text/javascript">        function showMsg()        {            //定時重新整理擷取聊天內容            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;                    //保持捲軸在最下面                    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("登入逾時,請重新登入!");                            window.location.href = ‘_login.aspx‘;                        }                        else {                            alert("通訊發送錯誤:" + result.d);                        }                    },                    error: function (e) {                        alert(e.d);                    }                });            }            else {                alert("請輸入內容再發送...");            }        }    </script>

  

  

asmx代碼:

[WebMethod (Description ="擷取聊天記錄",EnableSession = true)]        public string GetMessageContent()        {            if (Application["Message"] == null)            {                return "當前無聊天資訊。";            }            int length = Convert.ToInt32(Session["MessageLength"]);            return Application["Message"].ToString().Substring(length);        }        [WebMethod(Description = "發送聊天內容", EnableSession = true)]        public string SendMsg(string Msg)        {            //驗證使用者是否登入逾時            if (Session["UserName"] == null)            {                return "OutTime";            }            try            {                //使用者 [時間]:內容                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 = "擷取線上使用者")]        public string GetOnlineUsers()        {            if (Application["OnlinePersons"] == null)            {                return "";            }            return Application["OnlinePersons"].ToString();        }

  登入後cs代碼:

        protected void btnLogin_Click(object sender, EventArgs e)        {            //記錄使用者名稱            Session["username"] = username.Value;            //增加線上使用者            Application.Lock();            Application["online"] += username.Value + "|";            Application.UnLock();            //記錄登入時存在的聊天資訊長度,只顯示登入後發生的聊天資訊            Session["messageLength"] = 0;            Application.Lock();            Session["messageLength"] = Application["Message"].ToString().Length;            Application.UnLock();            //跳轉頁面            Response.Redirect("_showMessage.aspx");        }

  

 

原始碼連結:http://pan.baidu.com/s/1o7WuTGu

 

asp.net練習①——Application聊天室

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.