123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- //
- // XMPPController.swift
- // Chat
- //
- // Created by Sergey Tarasov on 25.07.2022.
- //
- import Foundation
- import XMPPFramework
- import XMPPFrameworkSwift
- final class XMPPController: NSObject {
- static let shared = XMPPController()
- var xmppStream: XMPPStream
- var xmppReconnect: XMPPReconnect
- var xmppRoster: XMPPRoster
- var xmppRosterStorage: XMPPRosterMemoryStorage
- var xmppCapabilities: XMPPCapabilities
- var xmppCapabilitiesStorage: XMPPCapabilitiesStorage
- var xmppPing: XMPPPing
- var xmppTime: XMPPTime
- var turnSockets: NSMutableArray
- var password: String?
- override init() {
- self.xmppStream = XMPPStream()
- self.xmppReconnect = XMPPReconnect()
- self.xmppRosterStorage = XMPPRosterMemoryStorage()
- self.xmppRoster = XMPPRoster(rosterStorage: self.xmppRosterStorage, dispatchQueue: DispatchQueue.main)
- self.xmppCapabilitiesStorage = XMPPCapabilitiesCoreDataStorage.sharedInstance()
- self.xmppCapabilities = XMPPCapabilities(capabilitiesStorage: self.xmppCapabilitiesStorage)
- self.xmppCapabilities.autoFetchHashedCapabilities = true
- self.xmppCapabilities.autoFetchNonHashedCapabilities = false
- self.xmppPing = XMPPPing()
- self.xmppTime = XMPPTime()
- self.xmppReconnect.activate(self.xmppStream)
- self.xmppRoster.activate(self.xmppStream)
- self.xmppCapabilities.activate(self.xmppStream)
- self.xmppPing.activate(self.xmppStream)
- self.xmppTime.activate(self.xmppStream)
- self.turnSockets = []
- super.init()
- self.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
- self.xmppReconnect.addDelegate(self, delegateQueue: DispatchQueue.main)
- self.xmppCapabilities.addDelegate(self, delegateQueue: DispatchQueue.main)
- }
- func setupStream(with login: String?, password: String?) {
- self.password = password ?? ""
- self.xmppStream.hostName = "msg.sharix-app.org"
- self.xmppStream.hostPort = 5222
- self.xmppStream.myJID = XMPPJID(string: login ?? "test11@msg.sharix-app.org")
- self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
- }
- func connect() {
- if !self.xmppStream.isDisconnected { return }
- try! self.xmppStream.connect(withTimeout: XMPPStreamTimeoutNone)
- }
- }
- extension XMPPController: TURNSocketDelegate {
- func connectViaXEP65(with jid: XMPPJID) {
- let turnSocket = TURNSocket(stream: self.xmppStream, to: jid)
- self.turnSockets.add(turnSocket)
- turnSocket.start(with: self, delegateQueue: DispatchQueue.main)
- }
- func xmppStream(_ sender: XMPPStream, didReceive iq: XMPPIQ) -> Bool {
- print("---------- xmppStream:didReceiveIQ: ----------")
- if TURNSocket.isNewStartTURNRequest(iq) {
- let turnSocket = TURNSocket(stream: self.xmppStream, incomingTURNRequest: iq)
- self.turnSockets.add(turnSocket)
- turnSocket.start(with: self, delegateQueue: DispatchQueue.main)
- return true
- }
- return false
- }
- func turnSocket(_ sender: TURNSocket, didSucceed socket: GCDAsyncSocket) {
- self.turnSockets.remove(sender)
- }
- func turnSocketDidFail(_ sender: TURNSocket) {
- self.turnSockets.remove(sender)
- }
- }
- extension XMPPController: XMPPReconnectDelegate {
- func xmppReconnect(
- _ sender: XMPPReconnect,
- shouldAttemptAutoReconnect connectionFlags: SCNetworkConnectionFlags
- ) -> Bool {
- return true
- }
- }
- extension XMPPController: XMPPCapabilitiesDelegate {
- func xmppCapabilities(
- _ sender: XMPPCapabilities,
- didDiscoverCapabilities caps: DDXMLElement,
- for jid: XMPPJID
- ) {
- print("---------- xmppCapabilities:didDiscoverCapabilities:forJID: ----------")
- print("jid: " + jid.debugDescription)
- print("capabilities: " + caps.debugDescription)
- }
- }
- extension XMPPController: XMPPStreamDelegate {
- func xmppStreamDidConnect(_ stream: XMPPStream) {
- print("Stream: Connected")
- try! stream.authenticate(withPassword: self.password ?? "test11_-")
- }
- func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
- self.xmppStream.send(XMPPPresence())
- print("Stream: Authenticated")
- }
- }
|