From c19b966d2342b2180c1a89f9f07abb115a99b0d1 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Thu, 2 Dec 2021 23:45:55 +1100 Subject: [PATCH] fix (directchat): auto disconnect self once conn closed (#663) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When closing a connection of clientA(using the command “/disconnect”), the connection is then closed on clientA’s side. However, the connection remained open on clientB and clientB could continue sending message to clientA and clientA would receive those messages. This PR listens for a closing connection on clientB, then also closes clientB’s connection. --- examples/directchat.nim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/directchat.nim b/examples/directchat.nim index 225a9d5f1..c1ec410d8 100644 --- a/examples/directchat.nim +++ b/examples/directchat.nim @@ -79,7 +79,9 @@ proc handlePeer(c: Chat, conn: Connection) {.async.} = c.writeStdout $conn.peerId & ": " & $str except LPStreamEOFError: - c.writeStdout $conn.peerId & " disconnected" + defer: c.writeStdout $conn.peerId & " disconnected" + await c.conn.close() + c.connected = false proc dialPeer(c: Chat, address: string) {.async.} = # Parse and dial address @@ -130,7 +132,7 @@ proc readLoop(c: Chat) {.async.} = await c.switch.stop() c.writeStdout "quitting..." - quit(0) + return else: if c.connected: await c.conn.writeLp(line) @@ -189,5 +191,6 @@ proc main() {.async.} = await chat.readLoop() await allFuturesThrowing(libp2pFuts) + quit(0) waitFor(main())