Created
October 24, 2016 11:59
-
-
Save aisk/065dae7db395bcd934c292c1fcebb718 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import asyncio | |
import websockets | |
from datetime import datetime | |
last_connect_time = datetime.now() | |
@asyncio.coroutine | |
def connect(): | |
url = sys.argv[1] | |
global last_connect_time | |
now = datetime.now() | |
print('connection sustained:', now - last_connect_time) | |
last_connect_time = now | |
result = yield from websockets.connect(url) | |
return result | |
@asyncio.coroutine | |
def hello(): | |
msg = 'fuck!' | |
ws = yield from connect() | |
while True: | |
yield from ws.send(msg) | |
# print(datetime.now(), '>', msg) | |
try: | |
msg = yield from ws.recv() | |
except websockets.exceptions.ConnectionClosed as e: | |
print('error:', e) | |
ws = yield from connect() | |
# else: | |
# print(datetime.now(), '<', msg) | |
time.sleep(1) | |
ws.close() | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(hello()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment