修改LumaQQ.NET,使其支持长消息发送

        默认LumaQQ.NET不支持发送长消息,只能文字量比较小的文字信息。其实在内部设计上,已经支持了分片发送的功能,只是没有去拆分实现。我们这就来自己实现它。

1.首先在QQGlobal里添加消息最长长度定义:

   1: /// <summary>
   2: /// 消息最大长度,QQ是最大699字节   LumaQQ里说最长是700,但是我试下来只能是699,700就会出错,不知道为什么
   3: /// </summary>
   4: public const int QQ_MAX_SEND_IM = 699;

 

2.修改MessageManager里其中2个SendIM的方法

   1: /// <summary>
   2: /// 发送普通信息
   3: ///  <remark>abu 2008-03-11 </remark>
   4: /// </summary>
   5: /// <param name="receiver">The receiver.</param>
   6: /// <param name="message">The message.</param>
   7: /// <param name="fontSytle">The font sytle.</param>
   8: public void SendIM(int receiver, string message, FontStyle fontSytle)
   9: {
  10:     int MaxByte = QQGlobal.QQ_MAX_SEND_IM;//取最长长度
  11:  
  12:  
  13:     if (Encoding.GetEncoding(QQGlobal.QQ_CHARSET_DEFAULT).GetBytes(message).Length > MaxByte)//判断是不是要分段发送
  14:     {
  15:         List<byte> messageBytes = new List<byte>();
  16:         messageBytes.AddRange(Utils.Util.GetBytes(message));
  17:         messageBytes.Add(0x20);//补一个空格,不补似乎也会出问题
  18:         int messageSize = messageBytes.Count;
  19:  
  20:         int totalFragments = ((messageSize % MaxByte) > 0) ? (messageSize / MaxByte + 1) : (messageSize / MaxByte);//计算分片数
  21:         for (int fragementSequence = 0; fragementSequence < totalFragments; fragementSequence++)
  22:         {
  23:             int index = fragementSequence * MaxByte;
  24:             int BytesSize = ((messageSize - index) > MaxByte) ? MaxByte : (messageSize - index);//不能每次都申请最大长度的byte数组,不然字体会出问题
  25:             byte[] messageFragementBytes = new byte[BytesSize];
  26:  
  27:  
  28:             messageBytes.CopyTo(index, messageFragementBytes, 0, BytesSize);
  29:             SendIM(receiver, messageFragementBytes, totalFragments, fragementSequence, fontSytle);
  30:  
  31:  
  32:         }
  33:     }
  34:     else
  35:     {
  36:         SendIM(receiver, Utils.Util.GetBytes(message), 1, 0, fontSytle);
  37:     }
  38: }
  39: /// <summary>
  40: /// 发送普通信息
  41: ///  <remark>abu 2008-03-11 </remark>
  42: /// </summary>
  43: /// <param name="receiver">The receiver.</param>
  44: /// <param name="message">The message.</param>
  45: /// <param name="totalFragments">The total fragments.总分块数</param>
  46: /// <param name="fragementSequence">The fragement sequence.当前当块序号</param>
  47: /// <param name="fontSytle">The font sytle.</param>
  48: public void SendIM(int receiver, byte[] message, int totalFragments,
  49:     int fragementSequence, FontStyle fontSytle)
  50: {
  51:     SendIMPacket packet = new SendIMPacket(QQUser);
  52:     packet.Receiver = receiver;
  53:     packet.Message = message;//Utils.Util.GetBytes(message);
  54:     packet.TotalFragments = totalFragments;
  55:     packet.FragmentSequence = fragementSequence;
  56:     packet.FontStyle = fontSytle;
  57:     QQClient.PacketManager.SendPacket(packet, QQPort.Main.Name);
  58: }
posted @ 2008-04-22 13:17 蓝色的风之精灵 阅读(1623) 评论(15)  编辑 收藏

  回复  引用  查看    
#1楼 2008-04-22 13:40 | kkun      
这个东西倒底是做什么的?
是自己维护一个QQ服务器端程序,还是直接利用QQ服务器实现在线收发消息?
  回复  引用  查看    
#2楼 2008-04-22 13:43 | 李战      
又改进了啊,恭喜一下。
  回复  引用  查看    
#3楼 [楼主]2008-04-22 13:45 | 蓝色的风之精灵      
@kkun
参见博客园的LumaQQ.NET小组
  回复  引用  查看    
#4楼 2008-04-22 13:50 | 阿不      
谢谢楼主的实现,这个功能会直接添加到LumaQQ.NET里面。
  回复  引用  查看    
#5楼 [楼主]2008-04-22 14:10 | 蓝色的风之精灵      
突然发现我变成LumaQQ.NET小组的组长了……
  回复  引用  查看    
#6楼 2008-04-22 14:13 | chunfeng      
LumaQQ.NET源代码从那里下载,给我地址,我也想研究下
  回复  引用  查看    
#7楼 2008-04-22 14:34 | 随风逝去(叶进)      
雪中送炭啊!  谢谢lz
  回复  引用  查看    
#8楼 [楼主]2008-04-22 14:38 | 蓝色的风之精灵      
@chunfeng
http://space.cnblogs.com/group/lumaQQ_net/
这里是博客园的LumaQQ.NET的小组,里面有源代码的SVN地址
  回复  引用  查看    
#9楼 2008-04-22 14:41 | 阿不      
@蓝色的风之精灵
很早以前就是了,
你对LumaQQ.NET的贡献很大,谢谢!

另外,长信息和表情的功能我已经加入到LumaQQ.NET里面了,请签出测试一下。我目前没有测试环境可以测试,至少要保证在下个版本升级时功能正常。
  回复  引用  查看    
#10楼 2008-04-22 14:45 | TT.Net      
加油啊~
  回复  引用  查看    
#11楼 2008-04-22 15:54 | OneCool      
通过代理使用的搞定了吗?
  回复  引用  查看    
#12楼 [楼主]2008-04-22 16:52 | 蓝色的风之精灵      
@OneCool
那个……问问阿不同学吧……
  回复  引用  查看    
#13楼 2008-04-22 17:41 | 生鱼片      
这个现在挺火啊,谁弄个教程出来啊,就省着看了

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
另存  打印