diff --git a/Sources/PublicInterface/Client.swift b/Sources/PublicInterface/Client.swift index 00261131..be4d7e82 100644 --- a/Sources/PublicInterface/Client.swift +++ b/Sources/PublicInterface/Client.swift @@ -14,6 +14,7 @@ public protocol ClientDelegate: AnyObject { public protocol Client2Delegate: ClientDelegate { func client(_ client: Client, dappInfoForUrl url: WCURL) -> Session.DAppInfo? + func client(_ client: Client, willReconnect session: Session) } public class Client: WalletConnect { @@ -307,6 +308,12 @@ public class Client: WalletConnect { delegate?.client(self, didDisconnect: session) } + override func willReconnect(_ session: Session) { + if let delegate = delegate as? Client2Delegate { + delegate.client(self, willReconnect: session) + } + } + /// Thread-safe collection of client reponses private class Responses { diff --git a/Sources/PublicInterface/Server.swift b/Sources/PublicInterface/Server.swift index 680c0f47..dc97b20a 100644 --- a/Sources/PublicInterface/Server.swift +++ b/Sources/PublicInterface/Server.swift @@ -39,6 +39,10 @@ public protocol ServerDelegateV2: ServerDelegate { /// - requestId: connection request's id. Can be Int, Double, or String /// - session: the session to create. Contains dapp info received in the connection request. func server(_ server: Server, didReceiveConnectionRequest requestId: RequestID, for session: Session) + + /// Called when the session is being reconnected as part of the retry mechanism after the connection + /// has been lost due to e.g. bad connectivity. + func server(_ server: Server, willReconnect session: Session) } open class Server: WalletConnect { @@ -141,6 +145,12 @@ open class Server: WalletConnect { delegate?.server(self, didDisconnect: session) } + override func willReconnect(_ session: Session) { + if let delegate = delegate as? ServerDelegateV2 { + delegate.server(self, willReconnect: session) + } + } + /// Sends response for the create session request. /// Use this method together with `ServerDelegate.server(_ server:didReceiveConnectionRequest:for:)`. /// diff --git a/Sources/PublicInterface/WalletConnect.swift b/Sources/PublicInterface/WalletConnect.swift index b98291b1..3ee05bce 100644 --- a/Sources/PublicInterface/WalletConnect.swift +++ b/Sources/PublicInterface/WalletConnect.swift @@ -94,8 +94,8 @@ open class WalletConnect { } // if a session was not initiated by the wallet or the dApp to disconnect, try to reconnect it. guard communicator.pendingDisconnectSession(by: url) != nil else { - // TODO: should we notify delegate that we try to reconnect? LogService.shared.log("WC: trying to reconnect session by url: \(url.bridgeURL.absoluteString)") + willReconnect(session) try! reconnect(to: session) return } @@ -125,6 +125,10 @@ open class WalletConnect { preconditionFailure("Should be implemented in subclasses") } + func willReconnect(_ session: Session) { + preconditionFailure("Should be implemented in subclasses") + } + func log(_ request: Request) { guard let text = try? request.json().string else { return } LogService.shared.log("WC: <== [request] \(text)")