Skip to content
This repository has been archived by the owner on May 20, 2021. It is now read-only.

Persist session + connection timeout #7 #13

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions WalletConnect/WCInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ open class WCInteractor {
self.clientMeta = meta
self.socket = WebSocket.init(url: session.bridge)

socket.onConnect = { [weak self] in self?.onConnect() }
socket.onDisconnect = { [weak self] error in self?.onDisconnect(error: error) }
socket.onText = { [weak self] text in self?.onReceiveMessage(text: text) }
socket.onConnect = { [weak self] in self?.handleConnect() }
socket.onDisconnect = { [weak self] error in self?.handleDisconnect(error: error) }
socket.onText = { [weak self] text in self?.handleReceiveMessage(text: text) }
socket.onPong = { _ in print("<== pong") }
socket.onData = { data in print("<== websocketDidReceiveData: \(data.toHexString())") }
}
Expand Down Expand Up @@ -225,7 +225,7 @@ extension WCInteractor {

extension WCInteractor {

private func onConnect() {
private func handleConnect() {
print("<== websocketDidConnect")
pingTimer = Timer.scheduledTimer(withTimeInterval: 15, repeats: true, block: { [weak socket] _ in
print("==> ping")
Expand All @@ -237,10 +237,11 @@ extension WCInteractor {
connectResolver = nil
}

private func onDisconnect(error: Error?) {
private func handleDisconnect(error: Error?) {
print("<== websocketDidDisconnect, error: \(error.debugDescription)")
pingTimer?.invalidate()
if let error = error {
handleError(error: error)
connectResolver?.reject(error)
} else {
connectResolver?.fulfill(false)
Expand All @@ -249,7 +250,11 @@ extension WCInteractor {
onDisconnect?(error)
}

private func onReceiveMessage(text: String) {
private func handleError(error: Error) {
onError?(error)
}

private func handleReceiveMessage(text: String) {
print("<== receive: \(text)")
guard let (topic, payload) = WCEncryptionPayload.extract(text) else { return }
do {
Expand All @@ -267,7 +272,7 @@ extension WCInteractor {
}
}
} catch let error {
onError?(error)
handleError(error: error)
print("==> onReceiveMessage error: \(error.localizedDescription)")
}
}
Expand Down