-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f36fe6b
commit 74ca34c
Showing
13 changed files
with
235 additions
and
116 deletions.
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
57 changes: 57 additions & 0 deletions
57
Sources/Chat/ProtocolServices/Common/MessagingService.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 |
---|---|---|
@@ -1 +1,58 @@ | ||
import Foundation | ||
import WalletConnectUtils | ||
import Combine | ||
|
||
class MessagingService { | ||
let networkingInteractor: NetworkInteracting | ||
let logger: ConsoleLogging | ||
var onMessage: ((Message) -> Void)? | ||
private var publishers = [AnyCancellable]() | ||
|
||
init(networkingInteractor: NetworkInteracting, | ||
logger: ConsoleLogging) { | ||
self.networkingInteractor = networkingInteractor | ||
self.logger = logger | ||
setUpResponseHandling() | ||
setUpRequestHandling() | ||
} | ||
|
||
func send(topic: String, messageString: String) async throws { | ||
//TODO - manage author account | ||
let authorAccount = "TODO" | ||
let message = Message(message: messageString, authorAccount: authorAccount, timestamp: JsonRpcID.generate()) | ||
let request = JSONRPCRequest<ChatRequestParams>(params: .message(message)) | ||
try await networkingInteractor.request(request, topic: topic, envelopeType: .type0) | ||
} | ||
|
||
private func setUpResponseHandling() { | ||
networkingInteractor.responsePublisher | ||
.sink { [unowned self] response in | ||
switch response.requestParams { | ||
case .message: | ||
handleMessageResponse(response) | ||
default: | ||
return | ||
} | ||
}.store(in: &publishers) | ||
} | ||
|
||
private func setUpRequestHandling() { | ||
networkingInteractor.requestPublisher.sink { [unowned self] subscriptionPayload in | ||
switch subscriptionPayload.request.params { | ||
case .message(let message): | ||
handleMessage(message) | ||
default: | ||
return | ||
} | ||
}.store(in: &publishers) | ||
} | ||
|
||
private func handleMessage(_ message: Message) { | ||
onMessage?(message) | ||
logger.debug("Received message") | ||
} | ||
|
||
private func handleMessageResponse(_ response: ChatResponse) { | ||
logger.debug("Received Message response") | ||
} | ||
} |
Oops, something went wrong.