StreamManager.swift 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // StreamManager.swift
  3. // Chat (iOS)
  4. //
  5. // Created by Sergey Tarasov on 17.08.2022.
  6. //
  7. import Foundation
  8. import XMPPFramework
  9. import XMPPFrameworkSwift
  10. final class StreamManager: NSObject {
  11. static let shared = StreamManager()
  12. var stream: XMPPStream
  13. var delegate: XMPPStreamDelegate?
  14. var password: String
  15. override init() {
  16. self.stream = XMPPStream()
  17. self.password = ""
  18. super.init()
  19. self.stream.addDelegate(self, delegateQueue: DispatchQueue.main)
  20. }
  21. func setupStream(with login: String, password: String) {
  22. self.stream.hostName = "msg.sharix-app.org"
  23. self.stream.hostPort = 5222
  24. self.stream.myJID = XMPPJID(string: login)
  25. self.stream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
  26. self.password = password
  27. }
  28. func connect() {
  29. if !self.stream.isDisconnected { return }
  30. try! self.stream.connect(withTimeout: XMPPStreamTimeoutNone)
  31. }
  32. }
  33. extension StreamManager: XMPPStreamDelegate {
  34. func xmppStreamDidConnect(_ stream: XMPPStream) {
  35. print("Stream: Connected")
  36. try! stream.authenticate(withPassword: self.password)
  37. }
  38. func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
  39. self.stream.send(XMPPPresence())
  40. print("Stream: Authenticated")
  41. }
  42. }