Skip to content

Instantly share code, notes, and snippets.

@narodnik
Created January 15, 2020 12:52
Show Gist options
  • Save narodnik/cafa655c7e9387ddc4e63a6b8b1c169f to your computer and use it in GitHub Desktop.
Save narodnik/cafa655c7e9387ddc4e63a6b8b1c169f to your computer and use it in GitHub Desktop.
import asyncio
import json
import websockets
async def ping():
pong_address = "kauuj71-RPvETjz8FMQugnsNSDJ8033E4lNS_anMFD0="
uri = "ws://127.0.0.1:9001/mix"
async with websockets.connect(uri) as websock:
request_object = {
"type": "ownDetails"
}
await websock.send(json.dumps(request_object))
response = json.loads(await websock.recv())
print("Ping address =", response["address"])
request_object = {
"type": "send",
"message": "ping",
"recipient_address": pong_address
}
await websock.send(json.dumps(request_object))
print("Ping sent:", request_object)
async def pong():
ping_address = "lzdJ2EkCHlPFVLdsBCkW_n6thaql2wImrGMG0gt_EX4="
uri = "ws://127.0.0.1:9002/mix"
async with websockets.connect(uri) as websock:
request_object = {
"type": "ownDetails"
}
await websock.send(json.dumps(request_object))
response = json.loads(await websock.recv())
print("Ping address =", response["address"])
while True:
request_object = {
"type": "fetch"
}
await websock.send(json.dumps(request_object))
print("Pong sent:", request_object)
response = json.loads(await websock.recv())
messages = response["messages"]
print("Pong received messages:", messages)
if not messages:
await asyncio.sleep(1)
def main():
asyncio.get_event_loop().run_until_complete(asyncio.gather(
ping(), pong()
))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment