12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- //
- // XMPPController.swift
- // Chat (iOS)
- //
- // Created by Sergey Tarasov on 12.10.2022.
- //
- import XMPPFramework
- import XMPPFrameworkSwift
- final class XMPPController: NSObject {
- static let shared: XMPPController = XMPPController()
- let xmppStream: XMPPStream
- let xmppReconnect: XMPPReconnect
- let xmppRoster: XMPPRoster
- let xmppRosterStorage: XMPPRosterMemoryStorage
- let xmppMUCLight: XMPPMUCLight
- var xmppAuth: XMPPPlainAuthentication
- private override init() {
- self.xmppStream = XMPPStream()
- self.xmppReconnect = XMPPReconnect()
- self.xmppRosterStorage = XMPPRosterMemoryStorage()
- self.xmppRoster = XMPPRoster(rosterStorage: xmppRosterStorage)
- self.xmppMUCLight = XMPPMUCLight()
- self.xmppAuth = XMPPPlainAuthentication()
- self.xmppReconnect.activate(xmppStream)
- self.xmppRoster.activate(xmppStream)
- self.xmppMUCLight.activate(xmppStream)
- super.init()
- self.xmppStream.addDelegate(self, delegateQueue: .main)
- self.xmppReconnect.addDelegate(self, delegateQueue: .main)
- setupStream()
- }
- func setupStream() {
- self.xmppStream.hostName = "msg.sharix-app.org"
- self.xmppStream.hostPort = 5222
- self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
- }
- func login(with login: String, and password: String) {
- self.xmppStream.myJID = XMPPJID(string: login)
- self.xmppAuth = XMPPPlainAuthentication(stream: xmppStream, password: password)
- }
- func connect() {
- if self.xmppStream.isDisconnected {
- do {
- try xmppStream.connect(withTimeout: XMPPStreamTimeoutNone)
- } catch {
- }
- }
- }
- }
- extension XMPPController: XMPPReconnectDelegate {
- func xmppReconnect(_ sender: XMPPReconnect, shouldAttemptAutoReconnect connectionFlags: SCNetworkConnectionFlags) -> Bool {
- print("---------- xmppReconnect:shouldAttemptAutoReconnect: ----------")
- return true
- }
- }
- extension XMPPController: XMPPStreamDelegate {
- func xmppStreamDidConnect(_ sender: XMPPStream) {
- do {
- try self.xmppStream.authenticate(xmppAuth)
- } catch {
- }
- }
- func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
- sender.send(XMPPPresence())
- print("Stream: Authenticated")
- }
- }
|