mirror of https://github.com/vacp2p/nim-libp2p.git
fix (directchat): auto disconnect self once conn closed (#663)
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.
This commit is contained in:
parent
b8c54068a3
commit
c19b966d23
|
@ -79,7 +79,9 @@ proc handlePeer(c: Chat, conn: Connection) {.async.} =
|
||||||
c.writeStdout $conn.peerId & ": " & $str
|
c.writeStdout $conn.peerId & ": " & $str
|
||||||
|
|
||||||
except LPStreamEOFError:
|
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.} =
|
proc dialPeer(c: Chat, address: string) {.async.} =
|
||||||
# Parse and dial address
|
# Parse and dial address
|
||||||
|
@ -130,7 +132,7 @@ proc readLoop(c: Chat) {.async.} =
|
||||||
|
|
||||||
await c.switch.stop()
|
await c.switch.stop()
|
||||||
c.writeStdout "quitting..."
|
c.writeStdout "quitting..."
|
||||||
quit(0)
|
return
|
||||||
else:
|
else:
|
||||||
if c.connected:
|
if c.connected:
|
||||||
await c.conn.writeLp(line)
|
await c.conn.writeLp(line)
|
||||||
|
@ -189,5 +191,6 @@ proc main() {.async.} =
|
||||||
|
|
||||||
await chat.readLoop()
|
await chat.readLoop()
|
||||||
await allFuturesThrowing(libp2pFuts)
|
await allFuturesThrowing(libp2pFuts)
|
||||||
|
quit(0)
|
||||||
|
|
||||||
waitFor(main())
|
waitFor(main())
|
||||||
|
|
Loading…
Reference in New Issue