From 0b4324b7d9762b9ca2d3b735aa085589f629613a Mon Sep 17 00:00:00 2001 From: Lvykk <34474615+Lvykk@users.noreply.github.com> Date: Tue, 2 Nov 2021 10:11:45 +0800 Subject: [PATCH 1/6] Update WalletConnectSwift.podspec Update library dependencies --- WalletConnectSwift.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WalletConnectSwift.podspec b/WalletConnectSwift.podspec index 4f7f1ab6..acd11dac 100644 --- a/WalletConnectSwift.podspec +++ b/WalletConnectSwift.podspec @@ -17,5 +17,5 @@ Pod::Spec.new do |spec| spec.source_files = "Sources/**/*.swift" spec.requires_arc = true spec.dependency "CryptoSwift", "~> 1.4" - spec.dependency "Starscream", "~> 3.1" + spec.dependency "Starscream", "~> 4.0.4" end From 4e5b35127ad911e2b0259dd0f6b2654491f9d97c Mon Sep 17 00:00:00 2001 From: Dmitry Bespalov Date: Wed, 17 Nov 2021 15:41:43 +0100 Subject: [PATCH 2/6] updated gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e1a464ce..699b199b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ ### Xcode ### +.swiftpm/ .build/ build/ *.pbxuser From 45a1999b924102ea3bc0ba789f7c3f7e42ea76ad Mon Sep 17 00:00:00 2001 From: Lvyk Date: Tue, 30 Nov 2021 17:30:42 +0800 Subject: [PATCH 3/6] Solve the problem of delayed connection closure Compatible with starscream version to 4.0.4 --- .../contents.xcworkspacedata | 7 ++ Cartfile | 2 +- Package.resolved | 34 -------- Package.swift | 2 +- Sources/Internal/WebSocketConnection.swift | 77 ++++++++++++++----- Sources/PublicInterface/Server.swift | 2 + WalletConnectSwift.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 17 +--- 8 files changed, 75 insertions(+), 68 deletions(-) create mode 100644 .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata delete mode 100644 Package.resolved diff --git a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..919434a6 --- /dev/null +++ b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Cartfile b/Cartfile index 06a437e6..610ca599 100644 --- a/Cartfile +++ b/Cartfile @@ -1,2 +1,2 @@ github "krzyzanowskim/CryptoSwift" ~> 1.4.0 -github "daltoniam/Starscream" ~> 3.1.0 +github "daltoniam/Starscream" ~> 4.0.4 diff --git a/Package.resolved b/Package.resolved deleted file mode 100644 index 0583fbb4..00000000 --- a/Package.resolved +++ /dev/null @@ -1,34 +0,0 @@ -{ - "object": { - "pins": [ - { - "package": "CryptoSwift", - "repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git", - "state": { - "branch": null, - "revision": "4e31051c63cc0ddf10a25cf5318856c510cf77f4", - "version": "1.4.0" - } - }, - { - "package": "Starscream", - "repositoryURL": "https://github.com/daltoniam/Starscream.git", - "state": { - "branch": null, - "revision": "e6b65c6d9077ea48b4a7bdda8994a1d3c6969c8d", - "version": "3.1.1" - } - }, - { - "package": "swift-nio-zlib-support", - "repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git", - "state": { - "branch": null, - "revision": "37760e9a52030bb9011972c5213c3350fa9d41fd", - "version": "1.0.0" - } - } - ] - }, - "version": 1 -} diff --git a/Package.swift b/Package.swift index 26ca86fe..b143e68e 100644 --- a/Package.swift +++ b/Package.swift @@ -13,7 +13,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMinor(from: "1.4.0")), - .package(url: "https://github.com/daltoniam/Starscream.git", .upToNextMinor(from: "3.1.0")) + .package(url: "https://github.com/daltoniam/Starscream.git", .upToNextMinor(from: "4.0.4")) ], targets: [ .target( diff --git a/Sources/Internal/WebSocketConnection.swift b/Sources/Internal/WebSocketConnection.swift index cbf9ccee..76d26abe 100644 --- a/Sources/Internal/WebSocketConnection.swift +++ b/Sources/Internal/WebSocketConnection.swift @@ -7,6 +7,7 @@ import Starscream class WebSocketConnection { let url: WCURL + private var isConnected: Bool = false private let socket: WebSocket private let onConnect: (() -> Void)? private let onDisconnect: ((Error?) -> Void)? @@ -15,17 +16,18 @@ class WebSocketConnection { private var pingTimer: Timer? // TODO: make injectable on server creation private let pingInterval: TimeInterval = 30 - + private let engine: WSEngine + private var requestSerializer: RequestSerializer = JSONRPCSerializer() private var responseSerializer: ResponseSerializer = JSONRPCSerializer() - + // serial queue for receiving the calls. private let serialCallbackQueue: DispatchQueue - + var isOpen: Bool { - return socket.isConnected + return self.isConnected } - + init(url: WCURL, onConnect: (() -> Void)?, onDisconnect: ((Error?) -> Void)?, @@ -35,25 +37,30 @@ class WebSocketConnection { self.onDisconnect = onDisconnect self.onTextReceive = onTextReceive serialCallbackQueue = DispatchQueue(label: "org.walletconnect.swift.connection-\(url.bridgeURL)-\(url.topic)") - socket = WebSocket(url: url.bridgeURL) + if #available(macOS 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *) { + self.engine = WSEngine(transport: TCPTransport(), certPinner: FoundationSecurity()) + } else { + self.engine = WSEngine(transport: FoundationTransport(), certPinner: FoundationSecurity()) + } + socket = WebSocket(request: URLRequest(url: url.bridgeURL), engine: self.engine) socket.delegate = self socket.callbackQueue = serialCallbackQueue } - + func open() { socket.connect() } - + func close() { socket.disconnect() } - + func send(_ text: String) { - guard socket.isConnected else { return } + guard self.isConnected else { return } socket.write(string: text) log(text) } - + private func log(_ text: String) { if let request = try? requestSerializer.deserialize(text, url: url).json().string { LogService.shared.log("WC: ==> \(request)") @@ -66,24 +73,58 @@ class WebSocketConnection { } extension WebSocketConnection: WebSocketDelegate { - func websocketDidConnect(socket: WebSocketClient) { + + func didReceive(event: WebSocketEvent, client: WebSocket) { + switch event { + case .connected: + self.websocketDidConnect(socket: client) + break + case .disconnected, .cancelled: + self.websocketDidDisconnect(socket: client, error: nil) + break + case .text(let string): + self.websocketDidReceiveMessage(socket: client, text: string) + break + case .binary(let data): + self.websocketDidReceiveData(socket: client, data: data) + break + case .pong: + LogService.shared.log("WC: <== pong") + break + case .ping: + LogService.shared.log("WC: <== ping") + break + case .error(let error): + self.websocketDidDisconnect(socket: client, error: error) + break + case .reconnectSuggested: + LogService.shared.log("WC: <== reconnectSuggested") //TODO: Should we? + break + case .viabilityChanged: + break + } + } + + private func websocketDidConnect(socket: WebSocketClient) { pingTimer = Timer.scheduledTimer(withTimeInterval: pingInterval, repeats: true) { [weak self] _ in LogService.shared.log("WC: ==> ping") self?.socket.write(ping: Data()) } + self.isConnected = true onConnect?() } - - func websocketDidDisconnect(socket: WebSocketClient, error: Error?) { + + private func websocketDidDisconnect(socket: WebSocketClient, error: Error?) { pingTimer?.invalidate() + self.isConnected = false onDisconnect?(error) } - - func websocketDidReceiveMessage(socket: WebSocketClient, text: String) { + + private func websocketDidReceiveMessage(socket: WebSocketClient, text: String) { onTextReceive?(text) } - - func websocketDidReceiveData(socket: WebSocketClient, data: Data) { + + private func websocketDidReceiveData(socket: WebSocketClient, data: Data) { // no-op } } diff --git a/Sources/PublicInterface/Server.swift b/Sources/PublicInterface/Server.swift index dc23ce7d..43821056 100644 --- a/Sources/PublicInterface/Server.swift +++ b/Sources/PublicInterface/Server.swift @@ -178,6 +178,8 @@ extension Server: HandshakeHandlerDelegate { self.communicator.addOrUpdateSession(updatedSession) self.communicator.subscribe(on: walletInfo.peerId, url: updatedSession.url) self.delegate?.server(self, didConnect: updatedSession) + } else { + try? self.disconnect(from: Session(url: session.url, dAppInfo: session.dAppInfo, walletInfo: walletInfo)) } } } diff --git a/WalletConnectSwift.xcodeproj/project.pbxproj b/WalletConnectSwift.xcodeproj/project.pbxproj index 91b5d649..35b9ef7b 100644 --- a/WalletConnectSwift.xcodeproj/project.pbxproj +++ b/WalletConnectSwift.xcodeproj/project.pbxproj @@ -625,7 +625,7 @@ repositoryURL = "https://github.com/daltoniam/Starscream.git"; requirement = { kind = upToNextMinorVersion; - minimumVersion = 3.1.0; + minimumVersion = 4.0.4; }; }; /* End XCRemoteSwiftPackageReference section */ diff --git a/WalletConnectSwift.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/WalletConnectSwift.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 0583fbb4..420b02ad 100644 --- a/WalletConnectSwift.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/WalletConnectSwift.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git", "state": { "branch": null, - "revision": "4e31051c63cc0ddf10a25cf5318856c510cf77f4", - "version": "1.4.0" + "revision": "4b0565384d3c4c588af09e660535b2c7c9bf5b39", + "version": "1.4.2" } }, { @@ -15,17 +15,8 @@ "repositoryURL": "https://github.com/daltoniam/Starscream.git", "state": { "branch": null, - "revision": "e6b65c6d9077ea48b4a7bdda8994a1d3c6969c8d", - "version": "3.1.1" - } - }, - { - "package": "swift-nio-zlib-support", - "repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git", - "state": { - "branch": null, - "revision": "37760e9a52030bb9011972c5213c3350fa9d41fd", - "version": "1.0.0" + "revision": "df8d82047f6654d8e4b655d1b1525c64e1059d21", + "version": "4.0.4" } } ] From 69be8aed06b76856b623f474f9d036b29555c3a6 Mon Sep 17 00:00:00 2001 From: Lvyk Date: Thu, 2 Dec 2021 11:02:08 +0800 Subject: [PATCH 4/6] Fixed some issues 1.Fixed the problem of not calling the disconnected proxy method when the user refused 2.Fix the problem that after the user manually disconnects, the foreground will still try to reconnect after entering the picture from the background --- Sources/Internal/BridgeTransport.swift | 26 ++++++-- Sources/Internal/Communicator.swift | 4 +- Sources/Internal/WebSocketConnection.swift | 76 ++++++++++++++++++---- Sources/PublicInterface/Server.swift | 21 +++--- 4 files changed, 96 insertions(+), 31 deletions(-) diff --git a/Sources/Internal/BridgeTransport.swift b/Sources/Internal/BridgeTransport.swift index c0dc7201..731ef956 100644 --- a/Sources/Internal/BridgeTransport.swift +++ b/Sources/Internal/BridgeTransport.swift @@ -12,6 +12,7 @@ protocol Transport { onTextReceive: @escaping (String, WCURL) -> Void) func isConnected(by url: WCURL) -> Bool func disconnect(from url: WCURL) + func disconnect(from url: WCURL, usrDisconnect: Bool) } // future: if we received response from another peer - then we call request.completion() for pending request. @@ -24,7 +25,8 @@ class Bridge: Transport { // TODO: if no connection found, then what? func send(to url: WCURL, text: String) { dispatchPrecondition(condition: .notOnQueue(syncQueue)) - syncQueue.sync { [unowned self] in + syncQueue.sync { [weak self] in + guard let `self` = self else { return } if let connection = self.findConnection(url: url) { connection.send(text) } @@ -36,7 +38,8 @@ class Bridge: Transport { onDisconnect: @escaping ((WCURL, Error?) -> Void), onTextReceive: @escaping (String, WCURL) -> Void) { dispatchPrecondition(condition: .notOnQueue(syncQueue)) - syncQueue.sync { [unowned self] in + syncQueue.sync { [weak self] in + guard let `self` = self else { return } var connection: WebSocketConnection if let existingConnection = self.findConnection(url: url) { connection = existingConnection @@ -58,24 +61,35 @@ class Bridge: Transport { func isConnected(by url: WCURL) -> Bool { var connection: WebSocketConnection? dispatchPrecondition(condition: .notOnQueue(syncQueue)) - syncQueue.sync { [unowned self] in + syncQueue.sync { [weak self] in + guard let `self` = self else { return } connection = self.findConnection(url: url) } return connection?.isOpen ?? false } - + func disconnect(from url: WCURL) { + self.disconnect(from: url, usrDisconnect: false) + } + + func disconnect(from url: WCURL, usrDisconnect: Bool) { dispatchPrecondition(condition: .notOnQueue(syncQueue)) - syncQueue.sync { [unowned self] in + syncQueue.sync { [weak self] in + guard let `self` = self else { return } if let connection = self.findConnection(url: url) { + if usrDisconnect { + self.connections.removeAll { $0 === connection } + } connection.close() + } } } private func releaseConnection(by url: WCURL) { dispatchPrecondition(condition: .notOnQueue(syncQueue)) - syncQueue.sync { [unowned self] in + syncQueue.sync { [weak self] in + guard let `self` = self else { return } if let connection = self.findConnection(url: url) { self.connections.removeAll { $0 === connection } } diff --git a/Sources/Internal/Communicator.swift b/Sources/Internal/Communicator.swift index 513a2019..d5a3bbf2 100644 --- a/Sources/Internal/Communicator.swift +++ b/Sources/Internal/Communicator.swift @@ -45,8 +45,8 @@ class Communicator { return transport.isConnected(by: url) } - func disconnect(from url: WCURL) { - transport.disconnect(from: url) + func disconnect(from url: WCURL, usrDisconnect: Bool = false) { + transport.disconnect(from: url, usrDisconnect: usrDisconnect) } func pendingDisconnectSession(by url: WCURL) -> Session? { diff --git a/Sources/Internal/WebSocketConnection.swift b/Sources/Internal/WebSocketConnection.swift index 76d26abe..fb5d6564 100644 --- a/Sources/Internal/WebSocketConnection.swift +++ b/Sources/Internal/WebSocketConnection.swift @@ -16,7 +16,6 @@ class WebSocketConnection { private var pingTimer: Timer? // TODO: make injectable on server creation private let pingInterval: TimeInterval = 30 - private let engine: WSEngine private var requestSerializer: RequestSerializer = JSONRPCSerializer() private var responseSerializer: ResponseSerializer = JSONRPCSerializer() @@ -37,22 +36,49 @@ class WebSocketConnection { self.onDisconnect = onDisconnect self.onTextReceive = onTextReceive serialCallbackQueue = DispatchQueue(label: "org.walletconnect.swift.connection-\(url.bridgeURL)-\(url.topic)") - if #available(macOS 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *) { - self.engine = WSEngine(transport: TCPTransport(), certPinner: FoundationSecurity()) + var request = URLRequest(url: url.bridgeURL) + request.timeoutInterval = 5 + self.socket = WebSocket(request: request, engine: WSEngine(transport: FoundationTransport(), certPinner: FoundationSecurity())) + self.socket.callbackQueue = serialCallbackQueue + self.socket.delegate = self + +#if os(iOS) + var didBecomeActiveName: NSNotification.Name + if #available(iOS 13.0, *) { + didBecomeActiveName = UIScene.didActivateNotification } else { - self.engine = WSEngine(transport: FoundationTransport(), certPinner: FoundationSecurity()) + didBecomeActiveName = UIApplication.didBecomeActiveNotification } - socket = WebSocket(request: URLRequest(url: url.bridgeURL), engine: self.engine) - socket.delegate = self - socket.callbackQueue = serialCallbackQueue + NotificationCenter.default.addObserver(forName: didBecomeActiveName, + object: nil, + queue: OperationQueue.main) { [weak self] notification in + guard let `self` = self else { return } + self.open() + } + var willResignActiveName: NSNotification.Name + if #available(iOS 13.0, *) { + willResignActiveName = UIScene.willDeactivateNotification + } else { + willResignActiveName = UIApplication.willResignActiveNotification + } + NotificationCenter.default.addObserver(forName: willResignActiveName, + object: nil, + queue: OperationQueue.main) { [weak self] notification in + guard let `self` = self else { return } + print("WebSocketConnect: ==> Shotting down socket before background") + self.close(closeCode: CloseCode.normal.rawValue) + } +#endif + } func open() { socket.connect() } - func close() { - socket.disconnect() + func close(closeCode: UInt16 = CloseCode.normal.rawValue) { + self.socket.disconnect(closeCode: closeCode) + self.pingTimer?.invalidate() } func send(_ text: String) { @@ -70,6 +96,12 @@ class WebSocketConnection { LogService.shared.log("WC: ==> \(text)") } } + + deinit { +#if os(iOS) + NotificationCenter.default.removeObserver(self) +#endif + } } extension WebSocketConnection: WebSocketDelegate { @@ -80,7 +112,14 @@ extension WebSocketConnection: WebSocketDelegate { self.websocketDidConnect(socket: client) break case .disconnected, .cancelled: - self.websocketDidDisconnect(socket: client, error: nil) + DispatchQueue.main.sync { + if UIApplication.shared.applicationState == .active { + self.websocketDidDisconnect(socket: client, error: nil) + } else { + LogService.shared.log("WC: <== connection disconnected in background. Will re-activate when possible.") + } + } + break case .text(let string): self.websocketDidReceiveMessage(socket: client, text: string) @@ -95,7 +134,13 @@ extension WebSocketConnection: WebSocketDelegate { LogService.shared.log("WC: <== ping") break case .error(let error): - self.websocketDidDisconnect(socket: client, error: error) + DispatchQueue.main.async { + if UIApplication.shared.applicationState == .active { + self.websocketDidDisconnect(socket: client, error: error) + } else { + LogService.shared.log("WC: <== connection error in background. Will re-activate when possible.") + } + } break case .reconnectSuggested: LogService.shared.log("WC: <== reconnectSuggested") //TODO: Should we? @@ -106,9 +151,12 @@ extension WebSocketConnection: WebSocketDelegate { } private func websocketDidConnect(socket: WebSocketClient) { - pingTimer = Timer.scheduledTimer(withTimeInterval: pingInterval, repeats: true) { [weak self] _ in - LogService.shared.log("WC: ==> ping") - self?.socket.write(ping: Data()) + DispatchQueue.main.sync { + self.pingTimer = Timer.scheduledTimer(withTimeInterval: self.pingInterval, + repeats: true) { [weak self] _ in + LogService.shared.log("WC: ==> ping") + self?.socket.write(ping: Data()) + } } self.isConnected = true onConnect?() diff --git a/Sources/PublicInterface/Server.swift b/Sources/PublicInterface/Server.swift index 43821056..dd4fc3fc 100644 --- a/Sources/PublicInterface/Server.swift +++ b/Sources/PublicInterface/Server.swift @@ -138,7 +138,8 @@ open class Server: WalletConnect { func add(_ handler: RequestHandler) { dispatchPrecondition(condition: .notOnQueue(queue)) - queue.sync { [unowned self] in + queue.sync { [weak self] in + guard let `self` = self else { return } guard self.handlers.first(where: { $0 === handler }) == nil else { return } self.handlers.append(handler) } @@ -146,7 +147,8 @@ open class Server: WalletConnect { func remove(_ handler: RequestHandler) { dispatchPrecondition(condition: .notOnQueue(queue)) - queue.sync { [unowned self] in + queue.sync { [weak self] in + guard let `self` = self else { return } if let index = self.handlers.firstIndex(where: { $0 === handler }) { self.handlers.remove(at: index) } @@ -156,7 +158,8 @@ open class Server: WalletConnect { func find(by request: Request) -> RequestHandler? { var result: RequestHandler? dispatchPrecondition(condition: .notOnQueue(queue)) - queue.sync { [unowned self] in + queue.sync { [weak self] in + guard let `self` = self else { return } result = self.handlers.first { $0.canHandle(request: request) } } return result @@ -179,7 +182,9 @@ extension Server: HandshakeHandlerDelegate { self.communicator.subscribe(on: walletInfo.peerId, url: updatedSession.url) self.delegate?.server(self, didConnect: updatedSession) } else { - try? self.disconnect(from: Session(url: session.url, dAppInfo: session.dAppInfo, walletInfo: walletInfo)) + self.communicator.addOrUpdatePendingDisconnectSession(session) + self.communicator.disconnect(from: session.url, usrDisconnect: true) + self.delegate?.server(self, didDisconnect: session) } } } @@ -189,11 +194,9 @@ extension Server: UpdateSessionHandlerDelegate { func handler(_ handler: UpdateSessionHandler, didUpdateSessionByURL url: WCURL, sessionInfo: SessionInfo) { guard let session = communicator.session(by: url) else { return } if !sessionInfo.approved { - do { - try disconnect(from: session) - } catch { // session already disconnected - delegate?.server(self, didDisconnect: session) - } + self.communicator.addOrUpdatePendingDisconnectSession(session) + self.communicator.disconnect(from: session.url, usrDisconnect: true) + self.delegate?.server(self, didDisconnect: session) } else { // we do not add sessions without walletInfo let walletInfo = session.walletInfo! From 5103d87f1f6410bb4478bc87e66d2ae00e5b81fe Mon Sep 17 00:00:00 2001 From: Lvyk Date: Thu, 2 Dec 2021 17:17:48 +0800 Subject: [PATCH 5/6] Update version to 1.6.1 --- Sources/Internal/BridgeTransport.swift | 9 ---- Sources/Internal/Communicator.swift | 4 +- Sources/Internal/WebSocketConnection.swift | 51 ++----------------- Sources/PublicInterface/Server.swift | 4 +- WalletConnectSwift.podspec | 2 +- WalletConnectSwift.xcodeproj/project.pbxproj | 2 +- .../xcschemes/WalletConnectSwift.xcscheme | 2 +- 7 files changed, 10 insertions(+), 64 deletions(-) diff --git a/Sources/Internal/BridgeTransport.swift b/Sources/Internal/BridgeTransport.swift index 731ef956..f6026985 100644 --- a/Sources/Internal/BridgeTransport.swift +++ b/Sources/Internal/BridgeTransport.swift @@ -12,7 +12,6 @@ protocol Transport { onTextReceive: @escaping (String, WCURL) -> Void) func isConnected(by url: WCURL) -> Bool func disconnect(from url: WCURL) - func disconnect(from url: WCURL, usrDisconnect: Bool) } // future: if we received response from another peer - then we call request.completion() for pending request. @@ -69,19 +68,11 @@ class Bridge: Transport { } func disconnect(from url: WCURL) { - self.disconnect(from: url, usrDisconnect: false) - } - - func disconnect(from url: WCURL, usrDisconnect: Bool) { dispatchPrecondition(condition: .notOnQueue(syncQueue)) syncQueue.sync { [weak self] in guard let `self` = self else { return } if let connection = self.findConnection(url: url) { - if usrDisconnect { - self.connections.removeAll { $0 === connection } - } connection.close() - } } } diff --git a/Sources/Internal/Communicator.swift b/Sources/Internal/Communicator.swift index d5a3bbf2..513a2019 100644 --- a/Sources/Internal/Communicator.swift +++ b/Sources/Internal/Communicator.swift @@ -45,8 +45,8 @@ class Communicator { return transport.isConnected(by: url) } - func disconnect(from url: WCURL, usrDisconnect: Bool = false) { - transport.disconnect(from: url, usrDisconnect: usrDisconnect) + func disconnect(from url: WCURL) { + transport.disconnect(from: url) } func pendingDisconnectSession(by url: WCURL) -> Session? { diff --git a/Sources/Internal/WebSocketConnection.swift b/Sources/Internal/WebSocketConnection.swift index fb5d6564..10219e85 100644 --- a/Sources/Internal/WebSocketConnection.swift +++ b/Sources/Internal/WebSocketConnection.swift @@ -37,39 +37,10 @@ class WebSocketConnection { self.onTextReceive = onTextReceive serialCallbackQueue = DispatchQueue(label: "org.walletconnect.swift.connection-\(url.bridgeURL)-\(url.topic)") var request = URLRequest(url: url.bridgeURL) - request.timeoutInterval = 5 self.socket = WebSocket(request: request, engine: WSEngine(transport: FoundationTransport(), certPinner: FoundationSecurity())) self.socket.callbackQueue = serialCallbackQueue self.socket.delegate = self -#if os(iOS) - var didBecomeActiveName: NSNotification.Name - if #available(iOS 13.0, *) { - didBecomeActiveName = UIScene.didActivateNotification - } else { - didBecomeActiveName = UIApplication.didBecomeActiveNotification - } - NotificationCenter.default.addObserver(forName: didBecomeActiveName, - object: nil, - queue: OperationQueue.main) { [weak self] notification in - guard let `self` = self else { return } - self.open() - } - var willResignActiveName: NSNotification.Name - if #available(iOS 13.0, *) { - willResignActiveName = UIScene.willDeactivateNotification - } else { - willResignActiveName = UIApplication.willResignActiveNotification - } - NotificationCenter.default.addObserver(forName: willResignActiveName, - object: nil, - queue: OperationQueue.main) { [weak self] notification in - guard let `self` = self else { return } - print("WebSocketConnect: ==> Shotting down socket before background") - self.close(closeCode: CloseCode.normal.rawValue) - } -#endif - } func open() { @@ -96,12 +67,7 @@ class WebSocketConnection { LogService.shared.log("WC: ==> \(text)") } } - - deinit { -#if os(iOS) - NotificationCenter.default.removeObserver(self) -#endif - } + } extension WebSocketConnection: WebSocketDelegate { @@ -113,13 +79,8 @@ extension WebSocketConnection: WebSocketDelegate { break case .disconnected, .cancelled: DispatchQueue.main.sync { - if UIApplication.shared.applicationState == .active { - self.websocketDidDisconnect(socket: client, error: nil) - } else { - LogService.shared.log("WC: <== connection disconnected in background. Will re-activate when possible.") - } + self.websocketDidDisconnect(socket: client, error: nil) } - break case .text(let string): self.websocketDidReceiveMessage(socket: client, text: string) @@ -134,13 +95,7 @@ extension WebSocketConnection: WebSocketDelegate { LogService.shared.log("WC: <== ping") break case .error(let error): - DispatchQueue.main.async { - if UIApplication.shared.applicationState == .active { - self.websocketDidDisconnect(socket: client, error: error) - } else { - LogService.shared.log("WC: <== connection error in background. Will re-activate when possible.") - } - } + self.websocketDidDisconnect(socket: client, error: error) break case .reconnectSuggested: LogService.shared.log("WC: <== reconnectSuggested") //TODO: Should we? diff --git a/Sources/PublicInterface/Server.swift b/Sources/PublicInterface/Server.swift index dd4fc3fc..2c1844dd 100644 --- a/Sources/PublicInterface/Server.swift +++ b/Sources/PublicInterface/Server.swift @@ -183,7 +183,7 @@ extension Server: HandshakeHandlerDelegate { self.delegate?.server(self, didConnect: updatedSession) } else { self.communicator.addOrUpdatePendingDisconnectSession(session) - self.communicator.disconnect(from: session.url, usrDisconnect: true) + self.communicator.disconnect(from: session.url) self.delegate?.server(self, didDisconnect: session) } } @@ -195,7 +195,7 @@ extension Server: UpdateSessionHandlerDelegate { guard let session = communicator.session(by: url) else { return } if !sessionInfo.approved { self.communicator.addOrUpdatePendingDisconnectSession(session) - self.communicator.disconnect(from: session.url, usrDisconnect: true) + self.communicator.disconnect(from: session.url) self.delegate?.server(self, didDisconnect: session) } else { // we do not add sessions without walletInfo diff --git a/WalletConnectSwift.podspec b/WalletConnectSwift.podspec index acd11dac..8d7d4f0a 100644 --- a/WalletConnectSwift.podspec +++ b/WalletConnectSwift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "WalletConnectSwift" - spec.version = "1.6.0" + spec.version = "1.6.1" spec.summary = "A delightful way to integrate WalletConnect into your app." spec.description = <<-DESC WalletConnect protocol implementation for enabling communication between dapps and diff --git a/WalletConnectSwift.xcodeproj/project.pbxproj b/WalletConnectSwift.xcodeproj/project.pbxproj index 35b9ef7b..2e053370 100644 --- a/WalletConnectSwift.xcodeproj/project.pbxproj +++ b/WalletConnectSwift.xcodeproj/project.pbxproj @@ -265,7 +265,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1030; - LastUpgradeCheck = 1250; + LastUpgradeCheck = 1310; ORGANIZATIONNAME = "Gnosis Ltd."; TargetAttributes = { 0A92784F230BFB2600FDCC0D = { diff --git a/WalletConnectSwift.xcodeproj/xcshareddata/xcschemes/WalletConnectSwift.xcscheme b/WalletConnectSwift.xcodeproj/xcshareddata/xcschemes/WalletConnectSwift.xcscheme index e8b81a6a..12d5cf9d 100644 --- a/WalletConnectSwift.xcodeproj/xcshareddata/xcschemes/WalletConnectSwift.xcscheme +++ b/WalletConnectSwift.xcodeproj/xcshareddata/xcschemes/WalletConnectSwift.xcscheme @@ -1,6 +1,6 @@ Date: Fri, 3 Dec 2021 09:28:47 +0100 Subject: [PATCH 6/6] Bumped version --- WalletConnectSwift.podspec | 2 +- WalletConnectSwift/Info.plist | 2 +- WalletConnectSwiftTests/Info.plist | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/WalletConnectSwift.podspec b/WalletConnectSwift.podspec index 8d7d4f0a..53207cf1 100644 --- a/WalletConnectSwift.podspec +++ b/WalletConnectSwift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "WalletConnectSwift" - spec.version = "1.6.1" + spec.version = "1.6.2" spec.summary = "A delightful way to integrate WalletConnect into your app." spec.description = <<-DESC WalletConnect protocol implementation for enabling communication between dapps and diff --git a/WalletConnectSwift/Info.plist b/WalletConnectSwift/Info.plist index c0fcf6c3..80c2b81b 100644 --- a/WalletConnectSwift/Info.plist +++ b/WalletConnectSwift/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.6.1 + 1.6.2 CFBundleVersion $(CURRENT_PROJECT_VERSION) diff --git a/WalletConnectSwiftTests/Info.plist b/WalletConnectSwiftTests/Info.plist index 120b91dd..5db9a5d6 100644 --- a/WalletConnectSwiftTests/Info.plist +++ b/WalletConnectSwiftTests/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 1.6.1 + 1.6.2 CFBundleVersion 1