fixing chat (#151)

This commit is contained in:
Robert Zajac 2019-04-18 15:56:02 -04:00 committed by GitHub
parent cce226c714
commit 5d4b4cf525
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,26 +33,23 @@ async def write_data(stream):
async def run(port, destination):
external_ip = urllib.request.urlopen(
'https://v4.ident.me/').read().decode('utf8')
transport_opt = "/ip4/%s/tcp/%s" % (external_ip, port)
host = await new_node(
transport_opt=["/ip4/%s/tcp/%s" % (external_ip, port)])
transport_opt=[transport_opt])
await host.get_network().listen(multiaddr.Multiaddr(transport_opt))
if not destination: # its the server
async def stream_handler(stream):
asyncio.ensure_future(read_data(stream))
asyncio.ensure_future(write_data(stream))
host.set_stream_handler(PROTOCOL_ID, stream_handler)
port = None
ip = None
for listener in host.network.listeners.values():
for addr in listener.get_addrs():
ip = addr.value_for_protocol('ip4')
port = int(addr.value_for_protocol('tcp'))
if not port:
raise RuntimeError("was not able to find the actual local port")
print("Run './examples/chat/chat.py -p %s -d /ip4/%s/tcp/%s/p2p/%s' on another console.\n" %
(int(port) + 1, ip, port, host.get_id().pretty()))
(int(port) + 1, external_ip, port, host.get_id().pretty()))
print("\nWaiting for incoming connection\n\n")
else: # its the client