使用ExtJS创建前端WebQQ界面

使用ExtJS创建前端WebQQ界面,使用ASP.NET处理数据存取,为了演示方便用Sqlite3存储数据。

功能概述
实现了最基础的一对一的通讯功能,实时收发信息,离线保存信息。来信自动弹出窗口。
实现思路
借鉴了早些年的聊天室思想。
获取聊天信息是客户端定时向服务器请求,按照发送的用户名来查询此用户是否有新信息。有信息则返回信息。如何获取?思路如下:
数据库中为每个用户设置一个 LastID ,获取信息的时候比较聊天信息表中最大的 ChatID 。如果 ChatID 大于 LastID ,则表明有新的聊天信息,接着从对应数据库获取聊天信息 ( ChatID-LastID 表明新信息条数 ), 返回给客户端。客户端再根据不同的用户名,将信息追加到相应的聊天窗口。这里还有一步,设置 LastID ,将这个最大的 ChatID 保存为 LastID
发送聊天信息比较好理解, POST 数据至服务器端的处理页面,进行保存即可。
实现细节
客户端
主要有4个处理函数:
双击用户名节点创建聊天窗口: webQQDblClick()
来信动态创建聊天窗口 : autoshowQQWindow()
定时从服务器获取聊天消息 : loadMessage()
向聊天窗口追加聊天信息: addMessage()
发送聊天信息的处理语句分别在 webQQDblClick() autoshowQQWindow()里面都有。当然这样处理是不符合一些设计思想的,多敲了很多代码。我是懒得封装成一个方法。
服务器端:
有两个处理页面:
一个保存提交的聊天信息页面:SendChatMessage.aspx
一个获取聊天信息的页面:GetChatMessage.aspx
主要的获取数据和保存信息的处理方法是在Chat类中定义的。

类图

注释说明

申明一下:我仅仅是实现了WebQQ的部分功能。并非实现合理。首先我的编码不是很规范。请谅解。其次WebQQ界面是ExtJS的Desktop例子 中的那个。稍稍把代码改一下,就可以独立了!还有QQ好友不是异步动态获取的。           我为每个用户都创建了一个数据库文件,这是因为Sqlite操作方便。还有每个用户都有很多不同的数据要存储,不仅仅是保存了聊天信息,所以才这样的。其 实你可以把Sqlite看成是一个文本文件,只不过是用关系数据库的方式来思考和存取。想象一下,如果你的一个文本文件,有10M大小。你用文本编辑器打 开时什么感觉。我宁可把它分解成50个,500个文本进行读取。这是我的看法!

还有使用Sqlite是为了演示方便,不需要任何安装、配置、管理。
(其他数据库估计就不能这样了!如果我用MSSQL也绝不会像处理Sqlite这样为每个用户创建一个数据库,因时因地而异。)

我还是先贴个图,诱惑一下。^_^

 

WebQQ主界面

聊天窗口

 

这里也有全部源代码地址:http://download.csdn.net/source/499268 下面贴部分代码:


  1. 服务器端消息处理页面:

  1. GetChatMessage.aspx
    1. public partial class GetChatMessage : System.Web.UI.Page 
    2. protected void Page_Load(object sender, EventArgs e) 
    3. string userinfoPath = Request.MapPath(@"//db/userinfo.db3"); 
    4. string userPath = Request.MapPath(@"/db/userdb/"); 

    5. string username = Request.Params["username"]; 
    6. if (username != null
    7. IChat chat = new Chat(userinfoPath, userPath); 
    8. string str = chat.GetChatMessage(username); 
    9. Response.Write(str); 
    10. Response.End(); 
    11. public partial class GetChatMessage : System.Web.UI.Page
    12. {
    13.     protected void Page_Load(object sender, EventArgs e)
    14.     {
    15.         string userinfoPath = Request.MapPath(@"//db/userinfo.db3");
    16.         string userPath = Request.MapPath(@"/db/userdb/");

    17.         string username = Request.Params["username"];
    18.         if (username != null)
    19.         {
    20.             IChat chat = new Chat(userinfoPath, userPath);
    21.             string str = chat.GetChatMessage(username);
    22.             Response.Write(str);
    23.         }
    24.         Response.End();
    25.     }

    26. SendChatMessage.aspx 
    27. SendChatMessage.aspx 

    28. public partial class SendChatMessage : System.Web.UI.Page 
    29. protected void Page_Load(object sender, EventArgs e) 
    30. string userinfoPath = Request.MapPath(@"/db/userinfo.db3"); 
    31. string userPath = Request.MapPath(@"/db/userdb/"); 

    32. string fromusername = Request.Params["fromusername"]; 
    33. string tousername = Request.Params["tousername"]; 
    34. string message = Request.Params["message"]; 
    35. if (fromusername != null && tousername != null && message != null
    36. IChat chat = new Chat(userinfoPath,userPath); 
    37. chat.SendChatMessage(fromusername, tousername, message); 
    38. Response.Write("{success:true}"); 
    39. else 
    40. Response.Write("{success:false}"); 
    41. Response.End(); 

    聊天消息处理接口:
    1. interface IChat 
    2. string GetChatMessage(string userName); 
    3. void SendChatMessage(string toUserName, string fromUserName, string message); 
    1. 聊天消息处理类:

    1. (数据库,我临时使用Sqlite3,创建了一个userinfo.db3存储全部用户的信 息,并且创建chatlastid表,存储每个用户的LastChatID。我为每个用户都创建了一个个人的数据库文件,如 snail.db3,oo.db3.存储他自己的消息(chatmsg表).)
      1. public class Chat: IChat 
      2. private string userinfoDBPath = ""
      3. private string userDBPath = ""
      4. public Chat(string userinfoStr,string userStr) 
      5. this.userinfoDBPath = userinfoStr; 
      6. this.userDBPath = userStr; 
      7. /// <summary> 
      8. /// 根据用户名获取最后一次发送或接受消息的ID 
      9. /// </summary> 
      10. /// <param name="userName">欲获取其LastChatID的用户</param> 
      11. private int GetLastChatID(string userName) 
      12. int returnValue = -1; 
      13. using(SQLiteConnection conn = new SQLiteConnection("Data Source=" + userinfoDBPath)) 
      14. try 
      15. conn.Open(); 
      16. string strsql = "select [lastchatid] from [chatlastid] where username = @userName"
      17. SQLiteCommand cmd = new SQLiteCommand(strsql, conn); 
      18. cmd.Parameters.AddWithValue("@userName", userName); 
      19. object result = cmd.ExecuteScalar(); 
      20. conn.Clone(); 
      21. if (result == null
      22. SetDefaultLastChatID(userName); 
      23. else 
      24. returnValue = Convert.ToInt32(result); 
      25. catch (Exception ex) 
      26. finally 
      27. return returnValue; 
      28. /// <summary> 
      29. /// 如果用户在ChatLastID表中没有值,则设置其为默认值-1 
      30. /// </summary> 
      31. /// <param name="userName">用户名</param> 
      32. private void SetDefaultLastChatID(string userName) 
      33. int defaultLastID = -1; 
      34. using(SQLiteConnection conn = new SQLiteConnection("Data Source=" + userinfoDBPath)) 
      35. try 
      36. conn.Open(); 
      37. string strsql = "insert into chatlastid(username,lastchatid) values(@userName,@lastchatid)"
      38. SQLiteCommand cmd = new SQLiteCommand(strsql, conn); 
      39. cmd.Parameters.AddWithValue("@userName", userName); 
      40. cmd.Parameters.AddWithValue("@lastchatid", defaultLastID); 
      41. cmd.ExecuteNonQuery(); 
      42. catch (Exception ex) 
      43. finally 
      44. conn.Clone(); 
      45. /// <summary> 
      46. /// 获取某个用户的消息列表,string类型,并且在获取消息的同时设置LastID。 
      47. /// </summary> 
      48. /// <param name="userName">欲获取消息的用户名</param> 
      49. public string GetChatMessage(string userName) 
      50. string returnStr = ""
      51. int lastID = GetLastChatID(userName); 
      52. int freshID = GetFreshChatID(userName); 
      53. if (freshID > lastID) 
      54. string datapath = userDBPath + userName + @".db3"
      55. int getChatID = freshID - lastID; 
      56. using(SQLiteConnection conn = new SQLiteConnection("Data Source=" + datapath)) 
      57. DataSet ds = new DataSet(); 
      58. try 
      59. conn.Open(); 
      60. string strsql = "select [chatid],[fromusername],[message],[senddate] from (Select [chatid],[fromusername],[message],[senddate] From [chatmsg] order by chatid desc limit "+getChatID+") order by chatid asc"
      61. SQLiteDataAdapter sdap = new SQLiteDataAdapter(strsql , conn); 
      62. sdap.Fill(ds); 
      63. conn.Clone(); 
      64. //将DataSet转化为JSON格式 
      65. returnStr = JSONHelper.Convert2Json(ds); 
      66. //设置LastChatID值chatlastid Table 
      67. SetLastChatID(userName, freshID); 
      68. catch (Exception ex) 
      69. finally 
      70. return returnStr; 
      71. /// <summary> 
      72. /// 给指定用户名发送消息,保存至该用户个人数据库。(发送时间在插入数据的时候获取当前时间。) 
      73. /// </summary> 
      74. /// <param name="toUserName">消息接收者名称</param> 
      75. /// <param name="fromUserName">消息发送者名称</param> 
      76. /// <param name="message">消息内容</param> 
      77. public void SendChatMessage(string fromUserName, string toUserName, string message) 
      78. string senddate = DateTime.Now.ToString("u").TrimEnd('Z'); 
      79. string datapath = userDBPath + toUserName + @".db3"
      80. using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + datapath)) 
      81. try 
      82. conn.Open(); 
      83. string strsql = "insert into chatmsg(fromusername,message,senddate) values(@fromUserName,@message,@senddate);"
      84. SQLiteCommand cmd = new SQLiteCommand(strsql, conn); 
      85. cmd.Parameters.AddWithValue("@fromUserName", fromUserName); 
      86. cmd.Parameters.AddWithValue("@message", message); 
      87. cmd.Parameters.AddWithValue("@senddate", senddate); 
      88. cmd.ExecuteNonQuery(); 
      89. catch (Exception ex) 
      90. finally 
      91. conn.Clone(); 
      92. /// <summary> 
      93. /// 设置公用数据库ChatLastID中的某个用户的LastID 
      94. /// </summary> 
      95. /// <param name="userName">欲设置ChatLastID的用户</param> 
      96. private void SetLastChatID(string userName, int lastchatid) 
      97. using(SQLiteConnection conn = new SQLiteConnection("Data Source=" + userinfoDBPath)) 
      98. try 
      99. conn.Open(); 
      100. string strsql = "update [chatlastid] set [lastchatid] = @lastchatid where [username] = @userName"
      101. SQLiteCommand cmd = new SQLiteCommand(strsql, conn); 
      102. cmd.Parameters.AddWithValue("@userName", userName); 
      103. cmd.Parameters.AddWithValue("@lastchatid", lastchatid); 
      104. cmd.ExecuteNonQuery(); 
      105. catch (Exception ex) 
      106. finally 
      107. conn.Clone(); 
      108. /// <summary> 
      109. /// 获取最新的ChatID 
      110. /// </summary> 
      111. /// <param name="userName">欲获取某个用户的名称</param> 
      112. private int GetFreshChatID(string userName) 
      113. int returnValue = 0; 
      114. string datapath = userDBPath + userName + @".db3"
      115. using(SQLiteConnection conn = new SQLiteConnection("Data Source=" + datapath)) 
      116. try 
      117. conn.Open(); 
      118. string strsql = "Select max(chatid)  From [chatmsg];"
      119. SQLiteCommand cmd = new SQLiteCommand(strsql, conn); 
      120. returnValue = Convert.ToInt32(cmd.ExecuteScalar()); 
      121. catch (Exception ex) 
      122. finally 
      123. conn.Clone(); 
      124. return returnValue; 

      125. chatmsg 表:


      chatmsg 表:


      1. Create  [chatmsg](
      2. [chatid] INTEGER PRIMARY KEY NOT NULL
      3. ,[fromusername] nvarchar(20) NOT NULL
      4. ,[message] nvarchar(200)
      5. ,[senddate] nvarchar(20)
      6. );
      Create  [chatmsg](
      [chatid] INTEGER PRIMARY KEY NOT NULL
      ,[fromusername] nvarchar(20) NOT NULL
      ,[message] nvarchar(200)
      ,[senddate] nvarchar(20)
      ); 
              

      1. chatlastid 表:
      chatlastid 表:

      1. Create  [chatlastid](
      2. [username] nvarCHAR(20) PRIMARY KEY NOT NULL
      3. ,[lastchatid] INTEGER NOT NULL DEFAULT '0'
      4. );
      Create  [chatlastid](
      [username] nvarCHAR(20) PRIMARY KEY NOT NULL
      ,[lastchatid] INTEGER NOT NULL DEFAULT '0'
      );
              

      1. 这个WebQQ有很多的不足之处,很多编码思想,编码规范等等都由这个自己来的,怎么想就做么做了,呵呵。如果你有好的建议,意见,咱多聊聊。^_^

      2. <PRE class=jscript name="code"> </PRE> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值