聊天室AJAx讀取訊息

來源:互聯網
上載者:User

  讀取訊息:登入後讀取登入後的訊息,在首次擷取訊息的時候,得到最後一行的ID再次讀訊息的時候SQL語句去判斷>最後行ID的值…….

登入後擷取訊息:

在頁面建立

<asp:HiddenField ID="DATEnow" runat="server" />

if(!IsPostBack)
       {
           DATEnow.Value= DateTime.Now.ToString(); 首次運行建立登入時間賦值給DATEnow; 
      }

select f.Name as 'Formusername',t.Name as 'ToUsername',m.id,m.Fromuserid,m.TouserId,m.Message,m.recordTime,m.messageState,m.readflag from Message as m inner join Users as f on f.Id=m.FromUserId inner join Users as t on t.Id=m.ToUserId where m.recordtime>@getNowdate

<asp:HiddenField ID="lengthid" runat="server" />

WEBserver 中返回最後行的MSgs[MSgs.Count - 1].Id.ToString() 在js中得到var valueID=result[result.length-1].substring(result[result.length-1].lastIndexOf(',')+1)

再賦值給  document.getElementById("lengthid").value=valueID;

 

頁面擷取訊息;

頁面JS代碼: 

  function $(j){return document.getElementById(j);}
       function GetObjectListCallBacks(result)
        {
          if(result.length!=0){
         for(var i=0;i<result.length;i++)
         {
           var valuesss=  result[i];
           var Hed=$("lengthid").value; 

           var valuess=  result[i].substring(0, result[i].lastIndexOf(',')); //這裡是我傳回值中最後面加了個ID用截取 ‘,’前的內容  就訊息 

           var valueID=result[result.length-1].substring(result[result.length-1].lastIndexOf(',')+1);///這裡得到的是ID
           $("lengthid").value=valueID;
           //$("Lid").value=valueID;測試最後個ID
           if(Hed<=valueID)
           {
               var newFontElem = document.createElement("font");
               newFontElem.innerHTML =valuess+"<br/>"
               $("Chat").appendChild(newFontElem);
               $("Chat").scrollTop= $("Chat").scrollHeight;//每次收到訊息吧捲軸拉到最下面
           }
        }
        }
        }
        function getSTrijng()
        {
        var YY=$("lengthid").value;

        var XX =$("DATEnow").value; 

       JS.getKMSg(XX,YY,GetObjectListCallBacks); 

        setTimeout('getSTrijng()', 1000);
        }

頁面代碼:

<form id="form1" runat="server">
  <div>
      <asp:HiddenField ID="DATEnow" runat="server" />
      <asp:HiddenField ID="lengthid" runat="server" />
      <input type="text" id="Lid" />
      <div id="Chat">
      </div>
  </div>
  <asp:ScriptManager ID="ScriptManager1" runat="server">
      <Services>
          <asp:ServiceReference Path="JS.asmx" /> 
      </Services>
  </asp:ScriptManager>
  </form>

 

 

WEBserver代碼:

在wenserver 中加

[System.Web.Script.Services.ScriptService] //必須加上這句 不然JS會無法找到的
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class JS : System.Web.Services.WebService
{

    public JS()
    {

        //如果使用設計的組件,請取消注釋以下行
        //InitializeComponent();
    }
    [WebMethod]
    public List<String> getKMSg(String XX, String YY)
    {
        if (YY != "") //這個是沒有最後個ID的時候執行的BLL層的東東
        {
            List<string> listStr = new List<string>();
            StringBuilder geTMsglist = new StringBuilder();
            List<Message> MSgs = new List<Message>();
            MSgs = new BLL.MsgCtergor().readMSg(5, 9999, DateTime.Parse(XX), int.Parse(YY.ToString()));//關鍵語句再上面已經說出
            if (MSgs.Count > 0)
            {
                for (int i = 0; i < MSgs.Count; i++)
                {
                    String formname = MSgs[i].Formusername.ToString();
                    String Toname = MSgs[i].ToUsername.ToString();
                    String SEnddate = MSgs[i].RecordTime.ToString();
                    String Sendtext = "  " + formname + "對" + Toname + "說[" + SEnddate + "]<br />";
                    String tEXS = MSgs[i].Messag.ToString();
                    Sendtext += "<B>" + tEXS + "</B>," + MSgs[MSgs.Count - 1].Id.ToString() + "";
                    geTMsglist.Append(Sendtext);
                    listStr.Add(geTMsglist.ToString());
                }
            }
            return listStr;
        }
        else
        {

        //這個是首次登入後的訊息
            List<string> listStr = new List<string>();
            StringBuilder geTMsglist = new StringBuilder();
            List<Message> MSgs = new List<Message>();
            MSgs = new BLL.MsgCtergor().readMSg(0, 9999, DateTime.Parse(XX), 9999);//關鍵語句再上面已經說出
            if (MSgs.Count > 0)
            {
                for (int i = 0; i < MSgs.Count; i++)
                {
                    String formname = MSgs[i].Formusername.ToString();
                    String Toname = MSgs[i].ToUsername.ToString();
                    String SEnddate = MSgs[i].RecordTime.ToString();
                    String Sendtext = "  " + formname + "對" + Toname + "說[" + SEnddate + "]<br />";
                    String tEXS = MSgs[i].Messag.ToString();
                    Sendtext += "<B>" + tEXS + "</B>," + MSgs[MSgs.Count - 1].Id.ToString() + "";
                    geTMsglist.Append(Sendtext);
                    listStr.Add(geTMsglist.ToString());
                }

            }
            return listStr;
        }
    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.