-
Notifications
You must be signed in to change notification settings - Fork 172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#11 auto/manual socket connection #66
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
24849d5
Add Automatic connection handler
llbartekll eb2b227
add socket connection handler to dispatcher
llbartekll 6dcf93b
remove socket connection handling from wallet connect client
llbartekll 17753fe
add manual and automatic socket connection type
llbartekll 2179835
Add manual socket connection handler
llbartekll fdc11fc
Fix Tests
llbartekll 5b53903
move network monitoring to automatic socket connection handler
llbartekll 5eaeb15
Add manual socket connection tests
llbartekll 6c0f6e7
add automatic socket connection handler tests
llbartekll 935a2e2
Update tests for automatic socket connection
llbartekll 1d0c517
extract class into a file
llbartekll b1707eb
fix build
llbartekll a6d0ed4
invalidate task
llbartekll 0f4e3c2
fixes
llbartekll 1e6ed7f
fix race condition
llbartekll 022a7f5
segregate websocket session interfaces
llbartekll 8a0f2c2
revert client interface changes
llbartekll b9ac587
print data on socket message
llbartekll 19f41a5
Add initializers or waku and wc client
llbartekll e6d0bf8
update initialisers, add docs
llbartekll ada029a
rename Relayer
llbartekll 1e52194
add example with injectable relayer in dapp
llbartekll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
import Foundation | ||
#if os(iOS) | ||
import UIKit | ||
#endif | ||
|
||
protocol AppStateObserving { | ||
var onWillEnterForeground: (()->())? {get set} | ||
var onWillEnterBackground: (()->())? {get set} | ||
} | ||
|
||
class AppStateObserver: AppStateObserving { | ||
@objc var onWillEnterForeground: (() -> ())? | ||
|
||
@objc var onWillEnterBackground: (() -> ())? | ||
|
||
init() { | ||
subscribeNotificationCenter() | ||
} | ||
|
||
private func subscribeNotificationCenter() { | ||
#if os(iOS) | ||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(appWillEnterForeground), | ||
name: UIApplication.willEnterForegroundNotification, | ||
object: nil) | ||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(appWillEnterBackground), | ||
name: UIApplication.willResignActiveNotification, | ||
object: nil) | ||
#endif | ||
} | ||
|
||
@objc | ||
private func appWillEnterBackground() { | ||
onWillEnterBackground?() | ||
} | ||
|
||
@objc | ||
private func appWillEnterForeground() { | ||
onWillEnterForeground?() | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ class NetworkMonitor: NetworkMonitoring { | |
monitor.start(queue: monitorQueue) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Sources/Relayer/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
|
||
#if os(iOS) | ||
import UIKit | ||
#endif | ||
import Foundation | ||
|
||
class AutomaticSocketConnectionHandler: SocketConnectionHandler { | ||
enum Error: Swift.Error { | ||
case manualSocketConnectionForbidden | ||
case manualSocketDisconnectionForbidden | ||
} | ||
private var appStateObserver: AppStateObserving | ||
let socket: WebSocketConnecting | ||
private var networkMonitor: NetworkMonitoring | ||
private let backgroundTaskRegistrar: BackgroundTaskRegistering | ||
|
||
init(networkMonitor: NetworkMonitoring = NetworkMonitor(), | ||
socket: WebSocketConnecting, | ||
appStateObserver: AppStateObserving = AppStateObserver(), | ||
backgroundTaskRegistrar: BackgroundTaskRegistering = BackgroundTaskRegistrar()) { | ||
self.appStateObserver = appStateObserver | ||
self.socket = socket | ||
self.networkMonitor = networkMonitor | ||
self.backgroundTaskRegistrar = backgroundTaskRegistrar | ||
setUpStateObserving() | ||
setUpNetworkMonitoring() | ||
socket.connect() | ||
} | ||
|
||
private func setUpStateObserving() { | ||
appStateObserver.onWillEnterBackground = { [unowned self] in | ||
registerBackgroundTask() | ||
} | ||
|
||
appStateObserver.onWillEnterForeground = { [unowned self] in | ||
socket.connect() | ||
} | ||
} | ||
|
||
private func setUpNetworkMonitoring() { | ||
networkMonitor.onSatisfied = { [weak self] in | ||
self?.handleNetworkSatisfied() | ||
} | ||
networkMonitor.onUnsatisfied = { [weak self] in | ||
self?.handleNetworkUnsatisfied() | ||
} | ||
networkMonitor.startMonitoring() | ||
} | ||
|
||
func registerBackgroundTask() { | ||
backgroundTaskRegistrar.register(name: "Finish Network Tasks") { [unowned self] in | ||
endBackgroundTask() | ||
} | ||
} | ||
|
||
func endBackgroundTask() { | ||
socket.disconnect(with: .normalClosure) | ||
} | ||
|
||
func handleConnect() throws { | ||
throw Error.manualSocketConnectionForbidden | ||
} | ||
|
||
func handleDisconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws { | ||
throw Error.manualSocketDisconnectionForbidden | ||
} | ||
|
||
func handleNetworkUnsatisfied() { | ||
socket.disconnect(with: .goingAway) | ||
} | ||
|
||
func handleNetworkSatisfied() { | ||
if !socket.isConnected { | ||
socket.connect() | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Sources/Relayer/SocketConnectionHandler/BackgroundTaskRegistering.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
import Foundation | ||
#if os(iOS) | ||
import UIKit | ||
#endif | ||
|
||
protocol BackgroundTaskRegistering { | ||
func register(name: String, completion: @escaping ()->()) | ||
} | ||
|
||
class BackgroundTaskRegistrar: BackgroundTaskRegistering { | ||
#if os(iOS) | ||
private var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid | ||
#endif | ||
|
||
func register(name: String, completion: @escaping () -> ()) { | ||
#if os(iOS) | ||
backgroundTaskID = .invalid | ||
backgroundTaskID = UIApplication.shared.beginBackgroundTask (withName: name) { [unowned self] in | ||
UIApplication.shared.endBackgroundTask(backgroundTaskID) | ||
backgroundTaskID = .invalid | ||
completion() | ||
} | ||
#endif | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Sources/Relayer/SocketConnectionHandler/ManualSocketConnectionHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
import Foundation | ||
|
||
class ManualSocketConnectionHandler: SocketConnectionHandler { | ||
var socket: WebSocketConnecting | ||
|
||
init(socket: WebSocketConnecting) { | ||
self.socket = socket | ||
} | ||
|
||
func handleConnect() throws { | ||
socket.connect() | ||
} | ||
|
||
func handleDisconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws { | ||
socket.disconnect(with: closeCode) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Sources/Relayer/SocketConnectionHandler/SocketConnectionHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
import Foundation | ||
|
||
protocol SocketConnectionHandler { | ||
var socket: WebSocketConnecting {get} | ||
func handleConnect() throws | ||
func handleDisconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍