From 74e9cacd583d8f27e110a07d6c1102a4e35744b1 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 12 Aug 2021 13:54:44 +1000 Subject: [PATCH] Implement MockWebSocket --- Podfile | 5 ++ Podfile.lock | 6 +- Session.xcodeproj/project.pbxproj | 6 ++ SessionMessagingKit/Calls/MockWebSocket.swift | 57 +++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 SessionMessagingKit/Calls/MockWebSocket.swift diff --git a/Podfile b/Podfile index d6631360a..1b004a8b1 100644 --- a/Podfile +++ b/Podfile @@ -11,6 +11,7 @@ target 'Session' do pod 'PromiseKit', :inhibit_warnings => true pod 'PureLayout', '~> 3.1.8', :inhibit_warnings => true pod 'Reachability', :inhibit_warnings => true + pod 'SocketRocket', '~> 0.5.1', :inhibit_warnings => true pod 'Sodium', '~> 0.8.0', :inhibit_warnings => true pod 'WebRTC', '~> 63.11', :inhibit_warnings => true pod 'YapDatabase/SQLCipher', :git => 'https://github.com/loki-project/session-ios-yap-database.git', branch: 'signal-release', :inhibit_warnings => true @@ -26,6 +27,7 @@ target 'SessionShareExtension' do pod 'PromiseKit', :inhibit_warnings => true pod 'PureLayout', '~> 3.1.8', :inhibit_warnings => true pod 'SignalCoreKit', git: 'https://github.com/signalapp/SignalCoreKit.git', :inhibit_warnings => true + pod 'SocketRocket', '~> 0.5.1', :inhibit_warnings => true pod 'WebRTC', '~> 63.11', :inhibit_warnings => true pod 'YapDatabase/SQLCipher', :git => 'https://github.com/loki-project/session-ios-yap-database.git', branch: 'signal-release', :inhibit_warnings => true end @@ -33,6 +35,7 @@ end target 'SessionNotificationServiceExtension' do pod 'Curve25519Kit', git: 'https://github.com/signalapp/Curve25519Kit.git', :inhibit_warnings => true pod 'SignalCoreKit', git: 'https://github.com/signalapp/SignalCoreKit.git', :inhibit_warnings => true + pod 'SocketRocket', '~> 0.5.1', :inhibit_warnings => true pod 'WebRTC', '~> 63.11', :inhibit_warnings => true pod 'YapDatabase/SQLCipher', :git => 'https://github.com/loki-project/session-ios-yap-database.git', branch: 'signal-release', :inhibit_warnings => true end @@ -50,6 +53,7 @@ target 'SignalUtilitiesKit' do pod 'Reachability', :inhibit_warnings => true pod 'SAMKeychain', :inhibit_warnings => true pod 'SignalCoreKit', git: 'https://github.com/signalapp/SignalCoreKit.git', :inhibit_warnings => true + pod 'SocketRocket', '~> 0.5.1', :inhibit_warnings => true pod 'SwiftProtobuf', '~> 1.5.0', :inhibit_warnings => true pod 'WebRTC', '~> 63.11', :inhibit_warnings => true pod 'YapDatabase/SQLCipher', :git => 'https://github.com/loki-project/session-ios-yap-database.git', branch: 'signal-release', :inhibit_warnings => true @@ -71,6 +75,7 @@ target 'SessionMessagingKit' do pod 'Reachability', :inhibit_warnings => true pod 'SAMKeychain', :inhibit_warnings => true pod 'SignalCoreKit', git: 'https://github.com/signalapp/SignalCoreKit.git', :inhibit_warnings => true + pod 'SocketRocket', '~> 0.5.1', :inhibit_warnings => true pod 'Sodium', '~> 0.8.0', :inhibit_warnings => true pod 'SwiftProtobuf', '~> 1.5.0', :inhibit_warnings => true pod 'WebRTC', '~> 63.11', :inhibit_warnings => true diff --git a/Podfile.lock b/Podfile.lock index b917479f8..4507b28a4 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -44,6 +44,7 @@ PODS: - SignalCoreKit (1.0.0): - CocoaLumberjack - GRKOpenSSLFramework + - SocketRocket (0.5.1) - Sodium (0.8.0) - SQLCipher (4.4.0): - SQLCipher/standard (= 4.4.0) @@ -134,6 +135,7 @@ DEPENDENCIES: - Reachability - SAMKeychain - SignalCoreKit (from `https://github.com/signalapp/SignalCoreKit.git`) + - SocketRocket (~> 0.5.1) - Sodium (~> 0.8.0) - SwiftProtobuf (~> 1.5.0) - WebRTC (~> 63.11) @@ -153,6 +155,7 @@ SPEC REPOS: - PureLayout - Reachability - SAMKeychain + - SocketRocket - Sodium - SQLCipher - SwiftProtobuf @@ -204,6 +207,7 @@ SPEC CHECKSUMS: Reachability: 33e18b67625424e47b6cde6d202dce689ad7af96 SAMKeychain: 483e1c9f32984d50ca961e26818a534283b4cd5c SignalCoreKit: 4562b2bbd9830077439ca003f952a798457d4ea5 + SocketRocket: d57c7159b83c3c6655745cd15302aa24b6bae531 Sodium: 63c0ca312a932e6da481689537d4b35568841bdc SQLCipher: e434ed542b24f38ea7b36468a13f9765e1b5c072 SwiftProtobuf: 241400280f912735c1e1b9fe675fdd2c6c4d42e2 @@ -212,6 +216,6 @@ SPEC CHECKSUMS: YYImage: 6db68da66f20d9f169ceb94dfb9947c3867b9665 ZXingObjC: fdbb269f25dd2032da343e06f10224d62f537bdb -PODFILE CHECKSUM: e94e0a63e3b5609dad5b74fbb8e1266ccce2f011 +PODFILE CHECKSUM: 0f0ee15979921c085945dc1cacb0fd1fd380b77d COCOAPODS: 1.10.1 diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 0e0679185..2e3ab67d7 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -251,6 +251,7 @@ B8B558F126C4BB0600693325 /* CameraManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B558F026C4BB0600693325 /* CameraManager.swift */; }; B8B558F326C4CA4600693325 /* MockCallConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B558F226C4CA4600693325 /* MockCallConfig.swift */; }; B8B558F926C4CE6800693325 /* CallVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B558F826C4CE6800693325 /* CallVC.swift */; }; + B8B558FB26C4D25C00693325 /* MockWebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B558FA26C4D25C00693325 /* MockWebSocket.swift */; }; B8BB82A5238F627000BA5194 /* HomeVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8BB82A4238F627000BA5194 /* HomeVC.swift */; }; B8BC00C0257D90E30032E807 /* General.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8BC00BF257D90E30032E807 /* General.swift */; }; B8C2B2C82563685C00551B4D /* CircleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8C2B2C72563685C00551B4D /* CircleView.swift */; }; @@ -1223,6 +1224,7 @@ B8B558F026C4BB0600693325 /* CameraManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraManager.swift; sourceTree = ""; }; B8B558F226C4CA4600693325 /* MockCallConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCallConfig.swift; sourceTree = ""; }; B8B558F826C4CE6800693325 /* CallVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CallVC.swift; sourceTree = ""; }; + B8B558FA26C4D25C00693325 /* MockWebSocket.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockWebSocket.swift; sourceTree = ""; }; B8B5BCEB2394D869003823C9 /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; B8BAC75B2695645400EA1759 /* hr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = hr; path = hr.lproj/Localizable.strings; sourceTree = ""; }; B8BAC75C2695648500EA1759 /* sv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/Localizable.strings; sourceTree = ""; }; @@ -2363,6 +2365,7 @@ B806ECA026C4A7E4008BDA44 /* CallManager+UI.swift */, B806ECA226C4A8C6008BDA44 /* MockTURNServer.swift */, B8B558F226C4CA4600693325 /* MockCallConfig.swift */, + B8B558FA26C4D25C00693325 /* MockWebSocket.swift */, ); path = Calls; sourceTree = ""; @@ -4245,6 +4248,7 @@ "${BUILT_PRODUCTS_DIR}/PureLayout/PureLayout.framework", "${BUILT_PRODUCTS_DIR}/Reachability/Reachability.framework", "${BUILT_PRODUCTS_DIR}/SQLCipher/SQLCipher.framework", + "${BUILT_PRODUCTS_DIR}/SocketRocket/SocketRocket.framework", "${BUILT_PRODUCTS_DIR}/Sodium/Sodium.framework", "${PODS_ROOT}/WebRTC/WebRTC.framework", "${BUILT_PRODUCTS_DIR}/YYImage/YYImage.framework", @@ -4268,6 +4272,7 @@ "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/PureLayout.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Reachability.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SQLCipher.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SocketRocket.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Sodium.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/WebRTC.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/YYImage.framework", @@ -4755,6 +4760,7 @@ C32C5BEF256DC8EE003C73A2 /* OWSDisappearingMessagesJob.m in Sources */, C34A977425A3E34A00852C71 /* ClosedGroupControlMessage.swift in Sources */, B88FA7B826045D100049422F /* OpenGroupAPIV2.swift in Sources */, + B8B558FB26C4D25C00693325 /* MockWebSocket.swift in Sources */, C32C5E97256DE0CB003C73A2 /* OWSPrimaryStorage.m in Sources */, C32C5EB9256DE130003C73A2 /* OWSQuotedReplyModel+Conversion.swift in Sources */, C3A71D1F25589AC30043A11F /* WebSocketResources.pb.swift in Sources */, diff --git a/SessionMessagingKit/Calls/MockWebSocket.swift b/SessionMessagingKit/Calls/MockWebSocket.swift new file mode 100644 index 000000000..931800ec9 --- /dev/null +++ b/SessionMessagingKit/Calls/MockWebSocket.swift @@ -0,0 +1,57 @@ +import Foundation +import SocketRocket + +protocol MockWebSocketDelegate : AnyObject { + + func webSocketDidConnect(_ webSocket: MockWebSocket) + func webSocketDidDisconnect(_ webSocket: MockWebSocket) + func webSocket(_ webSocket: MockWebSocket, didReceive data: String) +} + +final class MockWebSocket : NSObject { + weak var delegate: MockWebSocketDelegate? + var socket: SRWebSocket? + + var isConnected: Bool { + return socket != nil + } + + func connect(url: URL) { + socket = SRWebSocket(url: url) + socket?.delegate = self + socket?.open() + } + + func disconnect() { + socket?.close() + socket = nil + delegate?.webSocketDidDisconnect(self) + } + + func send(data: Data) { + guard let socket = socket else { return } + socket.send(data) + } +} + +extension MockWebSocket : SRWebSocketDelegate { + + func webSocket(_ webSocket: SRWebSocket!, didReceiveMessage message: Any!) { + guard let message = message as? String else { return } + delegate?.webSocket(self, didReceive: message) + } + + func webSocketDidOpen(_ webSocket: SRWebSocket!) { + delegate?.webSocketDidConnect(self) + } + + func webSocket(_ webSocket: SRWebSocket!, didFailWithError error: Error!) { + SNLog("Web socket failed with error: \(error?.localizedDescription ?? "nil").") + self.disconnect() + } + + func webSocket(_ webSocket: SRWebSocket!, didCloseWithCode code: Int, reason: String!, wasClean: Bool) { + SNLog("Web socket closed.") + self.disconnect() + } +}