XMPPController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // XMPPController.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 25.07.2022.
  6. //
  7. import Foundation
  8. import XMPPFramework
  9. import XMPPFrameworkSwift
  10. final class XMPPController: NSObject {
  11. static let shared = XMPPController()
  12. var xmppStream: XMPPStream
  13. var xmppReconnect: XMPPReconnect
  14. var xmppRoster: XMPPRoster
  15. var xmppRosterStorage: XMPPRosterMemoryStorage
  16. var xmppCapabilities: XMPPCapabilities
  17. var xmppCapabilitiesStorage: XMPPCapabilitiesStorage
  18. var xmppPing: XMPPPing
  19. var xmppTime: XMPPTime
  20. var turnSockets: NSMutableArray
  21. var password: String?
  22. override init() {
  23. self.xmppStream = XMPPStream()
  24. self.xmppReconnect = XMPPReconnect()
  25. self.xmppRosterStorage = XMPPRosterMemoryStorage()
  26. self.xmppRoster = XMPPRoster(rosterStorage: self.xmppRosterStorage, dispatchQueue: DispatchQueue.main)
  27. self.xmppCapabilitiesStorage = XMPPCapabilitiesCoreDataStorage.sharedInstance()
  28. self.xmppCapabilities = XMPPCapabilities(capabilitiesStorage: self.xmppCapabilitiesStorage)
  29. self.xmppCapabilities.autoFetchHashedCapabilities = true
  30. self.xmppCapabilities.autoFetchNonHashedCapabilities = false
  31. self.xmppPing = XMPPPing()
  32. self.xmppTime = XMPPTime()
  33. self.xmppReconnect.activate(self.xmppStream)
  34. self.xmppRoster.activate(self.xmppStream)
  35. self.xmppCapabilities.activate(self.xmppStream)
  36. self.xmppPing.activate(self.xmppStream)
  37. self.xmppTime.activate(self.xmppStream)
  38. self.turnSockets = []
  39. super.init()
  40. self.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
  41. self.xmppReconnect.addDelegate(self, delegateQueue: DispatchQueue.main)
  42. self.xmppCapabilities.addDelegate(self, delegateQueue: DispatchQueue.main)
  43. }
  44. func setupStream(with login: String?, password: String?) {
  45. self.password = password ?? ""
  46. self.xmppStream.hostName = "msg.sharix-app.org"
  47. self.xmppStream.hostPort = 5222
  48. self.xmppStream.myJID = XMPPJID(string: login ?? "test11@msg.sharix-app.org")
  49. self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
  50. }
  51. func connect() {
  52. if !self.xmppStream.isDisconnected { return }
  53. try! self.xmppStream.connect(withTimeout: XMPPStreamTimeoutNone)
  54. }
  55. }
  56. extension XMPPController: TURNSocketDelegate {
  57. func connectViaXEP65(with jid: XMPPJID) {
  58. let turnSocket = TURNSocket(stream: self.xmppStream, to: jid)
  59. self.turnSockets.add(turnSocket)
  60. turnSocket.start(with: self, delegateQueue: DispatchQueue.main)
  61. }
  62. func xmppStream(_ sender: XMPPStream, didReceive iq: XMPPIQ) -> Bool {
  63. print("---------- xmppStream:didReceiveIQ: ----------")
  64. if TURNSocket.isNewStartTURNRequest(iq) {
  65. let turnSocket = TURNSocket(stream: self.xmppStream, incomingTURNRequest: iq)
  66. self.turnSockets.add(turnSocket)
  67. turnSocket.start(with: self, delegateQueue: DispatchQueue.main)
  68. return true
  69. }
  70. return false
  71. }
  72. func turnSocket(_ sender: TURNSocket, didSucceed socket: GCDAsyncSocket) {
  73. self.turnSockets.remove(sender)
  74. }
  75. func turnSocketDidFail(_ sender: TURNSocket) {
  76. self.turnSockets.remove(sender)
  77. }
  78. }
  79. extension XMPPController: XMPPReconnectDelegate {
  80. func xmppReconnect(
  81. _ sender: XMPPReconnect,
  82. shouldAttemptAutoReconnect connectionFlags: SCNetworkConnectionFlags
  83. ) -> Bool {
  84. return true
  85. }
  86. }
  87. extension XMPPController: XMPPCapabilitiesDelegate {
  88. func xmppCapabilities(
  89. _ sender: XMPPCapabilities,
  90. didDiscoverCapabilities caps: DDXMLElement,
  91. for jid: XMPPJID
  92. ) {
  93. print("---------- xmppCapabilities:didDiscoverCapabilities:forJID: ----------")
  94. print("jid: " + jid.debugDescription)
  95. print("capabilities: " + caps.debugDescription)
  96. }
  97. }
  98. extension XMPPController: XMPPStreamDelegate {
  99. func xmppStreamDidConnect(_ stream: XMPPStream) {
  100. print("Stream: Connected")
  101. try! stream.authenticate(withPassword: self.password ?? "test11_-")
  102. }
  103. func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
  104. self.xmppStream.send(XMPPPresence())
  105. print("Stream: Authenticated")
  106. }
  107. }