mirror of
https://github.com/storytold/EventCenterPlugin.git
synced 2026-10-09 00:09:42 +00:00
53 lines
1.2 KiB
Python
53 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import asyncio
|
|
import redis
|
|
import sys
|
|
import websockets
|
|
|
|
#redis_client = redis.Redis(host='localhost', port=6379, db=0)
|
|
|
|
pool = redis.ConnectionPool(host='localhost', port=6379, password='', db=0)
|
|
redis_pool = redis.StrictRedis(connection_pool=pool)
|
|
#redis_connection = redis_pool.redis_connect()
|
|
|
|
#pubsub = redis_connection.pubsub()
|
|
pubsub = redis_pool.pubsub()
|
|
pubsub.subscribe('testing')
|
|
pubsub.subscribe('foo')
|
|
|
|
#for message in pubsub.listen():
|
|
# if message['data'] == 'exit':
|
|
# sys.exit()
|
|
# print(message)
|
|
|
|
|
|
"""
|
|
async def hello(websocket, path):
|
|
name = await websocket.recv()
|
|
print(f"< {name}")
|
|
|
|
greeting = f"Hello {name}!"
|
|
|
|
await websocket.send(greeting)
|
|
print(f"> {greeting}")
|
|
|
|
start_server = websockets.serve(hello, "localhost", 8765)
|
|
|
|
asyncio.get_event_loop().run_until_complete(start_server)
|
|
asyncio.get_event_loop().run_forever()
|
|
"""
|
|
|
|
async def time(websocket, path):
|
|
while True:
|
|
now = datetime.datetime.utcnow().isoformat() + "Z"
|
|
await websocket.send(now)
|
|
await asyncio.sleep(random.random() * 3)
|
|
|
|
start_server = websockets.serve(time, "127.0.0.1", 5678)
|
|
|
|
asyncio.get_event_loop().run_until_complete(start_server)
|
|
asyncio.get_event_loop().run_forever()
|
|
|
|
|