diff --git a/examples/chat/chat.py b/examples/chat/chat.py index d988111..9c55687 100755 --- a/examples/chat/chat.py +++ b/examples/chat/chat.py @@ -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