Skip to content

Commit 6828431

Browse files
committed
Fix duplicated messages on server's chat
Server's delegate method was being called multiple times ( as many as the clients connected ). This led to duplicated messages in the server user interface chat
1 parent 34bd482 commit 6828431

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

PlaneTalk/Communication/TransmissionManager/ServerCommunicationManager.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ final class ServerCommunicationManager: ServerCommunicationInterface {
241241
func sendServerMessage(_ text: String) {
242242
let composedText = serverMessageFactory.generateServerMessage(from: text)
243243

244+
var didSendMessage = false
245+
244246
for event in kEvents {
245247
let fd = event.ident
246248

@@ -250,11 +252,15 @@ final class ServerCommunicationManager: ServerCommunicationInterface {
250252
if bytes < 0 {
251253
print("Error sending TCP Message")
252254
} else {
253-
print("Sent by server: \(composedText)")
254-
serverTCPCommunicationDelegate?.serverDidSendItsText(composedText)
255+
didSendMessage = true
255256
}
256257
}
257258
}
259+
260+
if didSendMessage {
261+
print("Sent by server: \(composedText)")
262+
serverTCPCommunicationDelegate?.serverDidSendItsText(composedText)
263+
}
258264
}
259265

260266
func closeAll() {

0 commit comments

Comments
 (0)