標籤:
Openfire服務可以做檔案傳輸方法可供選擇有:
- XEP-0047: in-band bytestreams:帶內位元組流傳輸協議 base64編碼後放入iq訊息,同普通文字訊息,編解碼需耗點資源效率一般,適用於較小檔案;
- XEP-0096: SI File Transfer:檔案傳輸流初始化協議
- 文檔中提到可自行選擇開發支援斷點續傳。當file元素包含range無素時,range包含offset、length兩個屬性,可以指出要傳送的資料位元於檔案的什麼位置。
- XEP-0065: SOCKS5 Bytestreams:帶外socks5代理位元組流傳輸協議
- 文檔中提到 如果發送端位於公網,則接收端直接連接發送端拉取資料。如果雙方都位於內網,則雙方串連Proxy 伺服器,通過Proxy 伺服器轉寄資料。
- 兩種應用情境原文:
- A direct connection in which the StreamHost is the Requester, as described under Direct Connection
- A mediated connection in which the StreamHost is a Proxy, as described under Mediated Connection
- XEP-0066: Out of Band Data:帶外資料轉送協議
具體看協議文檔,沒搞過;XEP-0096中提到XEP-0066的drawbacks,原文:
-
- It is not reliable.
- It does not work when one of the parties is behind a firewall.
- It provides limited metadata about files to be exchanged
- 還有一種,自建個檔案伺服器,發送方傳送檔案到檔案伺服器,上傳成功後產生相應的縮圖(如果是圖片檔案的話)推送完成的訊息給接收方,接收方收到訊息後,去下載相應的檔案,比較好理解,但我猜想每次比上述的方式多了次 磁碟IO;項目進度急得話,也是可以選用,可後續最佳化嘛。
我選的是XEP-0096和XEP-0065 :
XEP-0096對應的iOS版本檔案 地址,XEP-0065 iOS 版本也可用上述地址的版本
初始化XMPPStream時需要注意:
XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@", account, XMPPDomain]]; XMPPJID *jidWithResource = [jid jidWithNewResource:@"ios"]; [self.xmppStream setMyJID:jidWithResource];
普通JID格式如:[email protected] (形如 [email protected])
第二行帶上resource,不帶我的程式報錯:
<error code="503" type="cancel"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
應該是兩端的resource不一致
初始化XMPPSIFileTransfer相應代碼:
NSString *sessionId = [_xmppStream generateUUID]; _sifiletransfer = [[XMPPSIFileTransfer alloc] init]; _sifiletransfer.sid = sessionId; [_sifiletransfer addDelegate:self delegateQueue:dispatch_get_main_queue()]; [_sifiletransfer activate:_xmppStream];
傳送檔案:
- (void)sendImageMessage:(NSData *)imageData toAccount:(NSString *)account{ XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@/%@", account, XMPPDomain, [[_xmppStream myJID] resource]]]; [_sifiletransfer initiateFileTransferTo:jid withData:imageData];}
其中檔案接收方的JID完整格式:[email protected]/resource (形如 [email protected]/ios)
發送完成後在XMPPSIFileTransfer.h中的
@protocol XMPPSIFileTransferDelegate <NSObject>@required- (void)receivedImage:(NSData*)image from:(XMPPJID*)from;@end
可在你自己的檔案中實現該方法接受檔案
至此,我在內網可以通過此方式傳輸檔案,Mac下得openfire服務,可選用源碼或dmg方式 均可;
後來測試一端內網,一端外網出現錯誤,提示為:
<error code="404" type="cancel"><item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
openfire後台配置:
注意xmpp.proxy.externalip 填寫你測試伺服器的外網地址
路由器的7777連接埠也要映射到外網,
至此,內外網兩端傳送檔案測試也通過。
希望能幫到您。
iOS用戶端 Openfire服務 利用XEP-0065和XEP-0096 做out-of-band bytestream檔案傳輸