robbiehanson / XMPPFramework

An XMPP Framework in Objective-C for Mac and iOS
Other
5.91k stars 2.09k forks source link

XMPP connect will error connection-timeout #1154

Closed Imanmf closed 4 years ago

Imanmf commented 4 years ago

hello, everybody, I am using XMPPFramework and i can't connect to the server. I have this error : stream:error xmlns:stream="http://etherx.jabber.org/streams" connection-timeout xmlns="urn:ietf:params:xml:ns:xmpp-streams" text xmlns="urn:ietf:params:xml:ns:xmpp-streams" lang="en">Idle connection

this is my code :

import UIKit import XMPPFramework

class ViewController: UIViewController, XMPPStreamDelegate {

var stream:XMPPStream!
var xmppRoster: XMPPRoster!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let xmppRosterStorage = XMPPRosterCoreDataStorage()
    xmppRoster = XMPPRoster(rosterStorage: xmppRosterStorage)

    stream = XMPPStream()
    stream.addDelegate(self, delegateQueue: .main)
    xmppRoster.activate(stream)

    stream.hostPort = 5222
    stream.myJID = XMPPJID(string: "emad@chat.myaddress.net")

    do {
        print("start connect")
        try stream.connect(withTimeout: 30)
        print("request sent")
    }
    catch {
        print("catch")

    }

    let button = UIButton()
    button.backgroundColor = UIColor.red
    button.setTitle("SendMessage", for: .normal)
    button.frame = CGRect(x: 10, y: 100, width: view.bounds.width - 20, height: 40)
    button.addTarget(self, action: #selector(self.sendMessage), for: .touchUpInside)

    self.view.addSubview(button)
}

@objc func sendMessage() {

    let message = "Yo!"
    let senderJID = XMPPJID(string: "emad2@chat.myaddress.net")
    let msg = XMPPMessage(type: "chat", to: senderJID)

    msg?.addBody(message)
    stream.send(msg)
}

func xmppStreamWillConnect(sender: XMPPStream!) {
    print("will connect")
}

func xmppStreamConnectDidTimeout(_ sender: XMPPStream!) {
    print("timeout:")
}

func xmppStreamDidConnect(sender: XMPPStream!) {
    print("connected")

    do {
        try sender.authenticate(withPassword: "1235")
    }
    catch {
        print("catch")

    }

}

func xmppStreamDidAuthenticate(_ sender: XMPPStream!) {
    print("auth done")
    sender.send(XMPPPresence())
}

func xmppStream(_ sender: XMPPStream!, didNotAuthenticate error: DDXMLElement!) {
    print("dint not auth")
    print(error)
}

func xmppStream(sender: XMPPStream!, didReceivePresence presence: XMPPPresence!) {
    print(presence)
    let presenceType = presence.type()
    let username = sender.myJID.user
    let presenceFromUser = presence.from().user

    if presenceFromUser != username  {
        if presenceType == "available" {
            print("available")
        }
        else if presenceType == "subscribe" {
            self.xmppRoster.subscribePresence(toUser: presence.from())
        }
        else {
            print("presence type"); print(presenceType)
        }
    }

}

func xmppStream(sender: XMPPStream!, didSendMessage message: XMPPMessage!) {
    print("sent")
}

private func xmppStream(sender: XMPPStream!, didFailToSendIQ iq: XMPPIQ!, error: NSError!) {
    print("error")
}

func xmppStream(_ sender: XMPPStream!, didReceiveError error: DDXMLElement!) {
    print("\(error)")
}

private func xmppStream(sender: XMPPStream!, didFailToSendMessage message: XMPPMessage!, error: NSError!) {
    print("fail")
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func xmppStream(sender: XMPPStream!, didReceiveMessage message: XMPPMessage!) {
    print(message)
}

}

I have problem with the first step. connecting to server

Ogerets commented 4 years ago

I don't see stream.hostName specified. Only stream.hostPort.

Imanmf commented 4 years ago

I don't see stream.hostName specified. Only stream.hostPort.

Thank you for answer. I set both but it's the same error.

    stream.hostName = "chat.gahvare.net"
    stream.hostPort = 5222
    stream.myJID = XMPPJID(string: "emad@chat.gahvare.net")

My swift version is 5 and my target is ios 10

Imanmf commented 4 years ago

Finally, I fixed my issue. I add dd DDLog.add(DDTTYLogger.sharedInstance) to my codes and i get more information. I used web functions to connect and this has a problem. func xmppStreamDidConnect(sender: XMPPStream!) { ... } it was in the document. so I write this function again. but it was changed in swift 4. so new code was like this: func xmppStreamDidConnect(_ sender: XMPPStream) { ... } so, second function (ew function) has not "!" symbol after XMPPStream. So Swift thought of the second function as a new one. not override for did connect function. so when I was connected to the server, server was waiting for me and my function didn't anything and i had an error with idle connection message