XMPP文件传输协议(一)

来源:互联网 发布:天下网络盟重新城论坛 编辑:程序博客网 时间:2024/06/05 06:53

XEP-0096的实现(OC)


官方文档请参考:XEP-0096 - Jabber/XMPP中文翻译计划

XMPP的文件传输通常分两步,就像QQ传文件一样,先是将文件名 大小等等信息发给对方,对象接受就开始传输文件

第一步的就是XEP-0096这个协议;

第二步有几种方法实现:

1.socks5 bytestreams 对应的协议是XEP-0065,建立P2P连接传输文件流
2.In-Band Bytestreams(ibb) 对应的协议是XEP-0047,带内传输,好像是将文件Base64转成文本传输,效率较慢

3.google的Jingle(不了解,据说是对xmpp的扩展)


这里,我觉得第一种方法较为好。


下面是第一步中,XEP-0096的oc实现代码:

    NSXMLElement *si = [NSXMLElement elementWithName:@"si" xmlns:@"http://jabber.org/protocol/si"];    [si addAttributeWithName:@"id" stringValue:uuid];    [si addAttributeWithName:@"mime-type" stringValue:@"text/plain"];    [si addAttributeWithName:@"profile" stringValue:@"http://jabber.org/protocol/si/profile/file-transfer"];        NSXMLElement *file = [NSXMLElement elementWithName:@"file" xmlns:@"http://jabber.org/protocol/si/profile/file-transfer"];    [file addAttributeWithName:@"name" stringValue:@"test.txt"];    [file addAttributeWithName:@"size" stringValue:@"2012"];    NSXMLElement *des = [NSXMLElement elementWithName:@"desc" stringValue:@"this is a test file"];        [file addChild:des];        NSXMLElement *feature = [NSXMLElement elementWithName:@"feature" xmlns:@"http://jabber.org/protocol/feature-neg"];    NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"];    [x addAttributeWithName:@"type" stringValue:@"form"];    NSXMLElement *field = [NSXMLElement elementWithName:@"field"];    [field addAttributeWithName:@"var" stringValue:@"stream-method"];    [field addAttributeWithName:@"type" stringValue:@"list-single"];    NSXMLElement *option1 = [NSXMLElement elementWithName:@"option"];    NSXMLElement *value1 = [NSXMLElement elementWithName:@"value" stringValue:@"http://jabber.org/protocol/bytestreams"];    [option1 addChild:value1];    [field addChild:option1];        NSXMLElement *option2 = [NSXMLElement elementWithName:@"option"];    NSXMLElement *value2 = [NSXMLElement elementWithName:@"value" stringValue:@"http://jabber.org/protocol/ibb"];    [option2  addChild:value2];    [field addChild:option2];        [x addChild:field];    [feature addChild:x];    [si addChild:file];    [si addChild:feature];    [self updateDiscoUUID];        XMPPIQ *iq= [XMPPIQ iqWithType:@"set" to:[XMPPJID jidWithString:hadrCodeJidString] elementID:discoUUID child:si];        [iq addAttributeWithName:@"from" stringValue:[xmppStream.myJID full]] ;    [xmppStream sendElement:iq];


其中[XMPPJID jidWithString:hadrCodeJidString] 替换为你要发送的对方的XMPPJID对象。

测试过这个是木有问题的,至于下面的步骤怎么实现我还在看,稍后再贴出。

via:大黄

原创粉丝点击