基于 XMPP协议的服务器端 文件互传的简单实现

学习一下基于XMPP协议的文件传输,首先假设用户已经登录,这里假设Clinet Send对Clinet Receive 发送文件,这里记录了Clinet Send的发送和接收XML流记录

1、发送请求表示要向Clinet test发送文件,并发送文件大小和文件名

Xml代码 复制代码
  1. SEND: <iq xmlns="jabber:client" id="agsXMPP_6" to="Receive @localhost/" type="set"><si xmlns="http://jabber.org/protocol/si" profile="http://jabber.org/protocol/si/profile/file-transfer" id="b3b5f49c-8a94-49e2-908a-3de694b98af0"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="MiniClient.exe.config" size="382"><range /></file><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="form"><field type="list-single" var="stream-method"><option><value>http://jabber.org/protocol/byReceive reams</value></option></field></x></feature></si></iq>  

 

2、Receive 收到Send发实来的请求后,发送出响应流

Xml代码 复制代码
  1. RECV: <iq xmlns="jabber:client" to="Send@localhost" type="result" id="agsXMPP_6"><si xmlns="http://jabber.org/protocol/si" id="b3b5f49c-8a94-49e2-908a-3de694b98af0"><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="submit"><field var="stream-method"><value>http://jabber.org/protocol/byReceive reams</value></field></x></feature></si></iq>  

3、Send收到Receive 愿意接收的响应后,发出如下XML流

Xml代码 复制代码
  1. SEND: <iq xmlns="jabber:client" id="agsXMPP_7" to="Receive@localhost" type="set"><query xmlns="http://jabber.org/protocol/bySendreams" sid="71f1b875-4d84-429c-b39d-7dabcd400f3d"><streamhost jid="Send@localhost/MiniClient" host="localhost" port="1000" /><streamhost jid="proxy.ag-software.de" host="proxy.ag-software.de" port="7777" /></query></iq>  

 注意这里注要是代理的设置,agsXMPP 自带的客户端是使用代理传输数据的,所以必须对其做相应的修改,使其支持局域网内支持发送文件.

下面我们来看一下服务器端是如何路由消息的呢?
从上面所发送的XML流我们可以看出,实际上用户传送文件所发送的都是IQ节,我们必须对IQ节进行路由才能使其找到相应的接收者 ,所以我们需要在XmppServerConnection类中添加如下代码:

Java代码 复制代码
  1. private void ProcessIQ(IQ iq)   
  2.         {   
  3. //传送文件得理   
  4.   if (iq.Query == null)   
  5.             {   
  6.                 if(iq.Type == IqType.result)   
  7.                     this.Send(iq);   
  8.                 ProcessOobIQ(iq);   
  9.             }   
  10.             else  
  11.             if (iq.Query.GetType() == typeof(Auth))   
  12.             {   
  13.                 Auth auth = iq.Query as Auth;   
  14.                 this.Username = auth.Username.ToString();   
  15.   
  16.                 switch (iq.Type)   
  17.                 {   
  18.                     case IqType.get:   
  19.                         iq.SwitchDirection();   
  20.                         iq.Type = IqType.result;   
  21.                         auth.AddChild(new Element("password"));   
  22.                         auth.AddChild(new Element("digest"));   
  23.                         Send(iq);   
  24.                         break;   
  25.                     case IqType.set:   
  26.                         // Here we should verify the authentication credentials   
  27.                         if (AccountBus.CheckLogin(auth.Username, auth.Digest, this.SessionId))   
  28.                         {   
  29.                             iq.SwitchDirection();   
  30.                             iq.Type = IqType.result;   
  31.                             iq.Query = null;   
  32.                             Send(iq);   
  33.                             Console.WriteLine(auth.Username + "登录了" + "   登录时间:" + System.DateTime.Now.ToString());   
  34.                         }   
  35.                         else  
  36.                         {   
  37.                           //  iq.SwitchDirection();   
  38.                             iq.Type = IqType.error;   
  39.                             iq.Query = null;   
  40.                             Send(iq);   
  41.                         }   
  42.                         break;   
  43.                 }   
  44.   
  45.             }   
  46.             else if (iq.Query.GetType() == typeof(Roster))   
  47.             {   
  48.                 ProcessRosterIQ(iq);   
  49.             }//用代理传送文件处理   
  50.             else if(iq.Query.FirstChild !=null)   
  51.             {   
  52.                // iq.SwitchDirection();   
  53.                // //iq.Type = IqType.result;   
  54.                // //Send(iq);   
  55.                // iq.Type = IqType.set;   
  56.                 //this.Send(iq);   
  57.                 ProcessOobIQ(iq);   
  58.             }   
  59.   
  60. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值