【发布时间】:2018-06-11 13:55:29
【问题描述】:
我在我的项目中使用XMPPFramework,我正在尝试互相发送照片,但我被卡住了。
文件传输第一次运行良好,但在尝试发送另一个文件时,它失败并显示消息“传输已在进行中”。
我搜索了很多,但没有关于这个案例的描述/建议/答案。我该如何解决这个问题?
我的代码在这里:
按下按钮开始发送。
@IBAction func didPressedSendButton(_ sender: UIButton) {
if imageView.image == nil{
print("Select image first!")
return
}
if jidField.text == nil{
print("Set opponent jid first!")
return
}
if !XMPP.shared.stream.isConnected{
print("Stream is not connected!")
return
}
sender.isEnabled = false
let outgoingFileTransfer = XMPPOutgoingFileTransfer(dispatchQueue: .main)
outgoingFileTransfer.activate(XMPP.shared.stream)
outgoingFileTransfer.addDelegate(self, delegateQueue: .main)
let opponent = XMPPJID(string: jidField.text!)!
let recipient = XMPPJID(user: opponent.user, domain: opponent.domain, resource: "foo")
do{
outgoingFileTransfer.recipientJID = recipient
outgoingFileTransfer.outgoingData = UIImagePNGRepresentation(imageView.image!)
try outgoingFileTransfer.start()
try outgoingFileTransfer.send(
UIImagePNGRepresentation(imageView.image!),
named: imageView.image?.description ,
toRecipient: recipient,
description: "outgoingFileTransfer Description")
}catch{
print("Uh-oh... Something went wrong.")
print(error.localizedDescription)
}
}
委托方法
func xmppOutgoingFileTransfer(_ sender: XMPPOutgoingFileTransfer!, didFailWithError error: Error!) {
print("There was an error for sending a file. \(error.localizedDescription)")
sendButton.isEnabled = true
}
func xmppOutgoingFileTransferDidSucceed(_ sender: XMPPOutgoingFileTransfer!) {
print("Sending a file succeed. pray for receiving with no problem.")
sendButton.isEnabled = true
}
func xmppIncomingFileTransfer(_ sender: XMPPIncomingFileTransfer!, didFailWithError error: Error!) {
print("Incoming file transfer failed with error : ", error)
}
func xmppIncomingFileTransfer(_ sender: XMPPIncomingFileTransfer!, didReceiveSIOffer offer: XMPPIQ!) {
print("Incoming File Transfer did receive SI Offer. Accepting...")
print("Offer : ")
print(offer.prettyXMLString())
sender.acceptSIOffer(offer)
//offer.name
}
func xmppIncomingFileTransfer(_ sender: XMPPIncomingFileTransfer!, didSucceedWith data: Data!, named name: String!) {
print("I got something...")
if let receivedString = String(data: data, encoding: .utf8){
let alert = UIAlertController(title: "You\'ve got :", message: receivedString, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
print("OK, this is a text file.")
print("Incoming file transfer did Succeed. Hooray!")
}else if let receivedImage = UIImage(data: data){
receivedImageview.image = receivedImage
print("OK, this is an image file.")
print("Incoming file transfer did Succeed. Hooray!")
}else{
print("I got something, but I can\'t convert it. what is this?")
}
}
+++ 附加
我“成功”发送和接收第一个文件,但我无法发送第二个文件。它只是返回一条错误消息“传输已在进行中”,并且文件传输永远不会开始。
【问题讨论】:
-
你做第二个文件的速度有多快?您可能会遇到
transferSuccess和cleanUp之间的竞争条件。 -
我知道第二个文件的传输速度有多快。它不会尝试发送,只会显示一条错误消息。 “传输已在进行中”。但是,我使用小尺寸的图像进行测试(大约 300x300),我认为这个问题并不重要。
-
我的意思是,如果您在第一个真正完成之前开始第二个,它将无法正常工作。我查看了源代码。
-
是的,除非整个第一个完全传输,否则它不会完成,因为我没有输入代码来结束流。但我想要的是“如何在收到所有数据后结束流”。我找不到任何方法来做到这一点。
-
嗨@CyanLee我也面临这个问题。如果你知道这个问题的解决方案。
标签: swift xmppframework