Use secrets for websocket server

This commit is contained in:
Brandon Thomas
2021-03-03 03:46:32 -05:00
parent ae6eda6036
commit d949d71eee
4 changed files with 22 additions and 64 deletions
+2
View File
@@ -1,2 +1,4 @@
Binaries/ Binaries/
Intermediate/ Intermediate/
python/
secrets.toml
+15 -2
View File
@@ -6,10 +6,23 @@ I tried Redis PubSub first, but the plugin I found was buggy. I'm trying WebSock
## Installation ## Installation
1. Install the [BlueprintWebSocket](https://www.unrealengine.com/marketplace/en-US/product/blueprintwebsocket) 1. Install python and dependencies
```
# Windows works just as Linux,
python -m venv python
# Except for activating the venv
.\python\Scripts\Activate.ps1
# Then it's easy again,
pip install -r requirements.txt
```
2. Install the [BlueprintWebSocket](https://www.unrealengine.com/marketplace/en-US/product/blueprintwebsocket)
plugin (paid/commercial) from the Epic/Unreal Marketplace. It's not expensive and has great documentation. plugin (paid/commercial) from the Epic/Unreal Marketplace. It's not expensive and has great documentation.
2. You need to adjust the main project's build dependencies (`ProjectName.Build.cs`) to include the following, 3. You need to adjust the main project's build dependencies (`ProjectName.Build.cs`) to include the following,
``` ```
PublicDependencyModuleNames.AddRange(new string[] { PublicDependencyModuleNames.AddRange(new string[] {
BIN
View File
Binary file not shown.
+5 -62
View File
@@ -4,57 +4,9 @@ import aioredis
import asyncio import asyncio
import redis import redis
import sys import sys
import toml
import websockets import websockets
#redis_client = redis.Redis(host='localhost', port=6379, db=0)
# Redis pubsub (blocking)
"""
pool = redis.ConnectionPool(host='localhost', port=6379, password='', db=0)
redis_pool = redis.StrictRedis(connection_pool=pool)
pubsub = redis_pool.pubsub()
pubsub.subscribe('testing')
pubsub.subscribe('foo')
#for message in pubsub.listen():
# if message['data'] == 'exit':
# sys.exit()
# print(message)
"""
# Single request/response websocket
"""
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()
"""
# Infinitely publishing websocket server:
"""
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()
"""
from aioredis.pubsub import Receiver from aioredis.pubsub import Receiver
from aioredis.abc import AbcChannel from aioredis.abc import AbcChannel
@@ -64,6 +16,8 @@ CHANNELS = [
'testing', 'testing',
] ]
print('reading secrets', flush=True)
SECRETS = toml.load("secrets.toml")
print('defining server', flush=True) print('defining server', flush=True)
@@ -91,26 +45,15 @@ async def server(websocket, path):
asyncio.ensure_future(reader(mpsc)) asyncio.ensure_future(reader(mpsc))
redis = await aioredis.create_redis_pool('redis://localhost') redis = await aioredis.create_redis_pool(SECRETS['redis_url'])
#await redis.subscribe(mpsc.channel('channel:1'), mpsc.channel('channel:testing'), mpsc.channel('testing'))
channels = [mpsc.channel(ch) for ch in CHANNELS] channels = [mpsc.channel(ch) for ch in CHANNELS]
await redis.subscribe(*channels) await redis.subscribe(*channels)
print('starting websocket server', flush=True) print('starting websocket server', flush=True)
try: try:
while True: while True:
# Receive data from "the outside world" # TODO: Do we need to ping to keepalive?
#message = await websocket.recv()
# Feed this data to the PUBLISH co-routine
#await publish_to_redis(message, path)
#print('sending ping', flush=True)
#await websocket.send("Websocket ping")
if not queue.empty(): if not queue.empty():
msg = queue.get_nowait() # TODO THROWS EXCEPTION IF EMPTY (data race?) msg = queue.get_nowait() # TODO THROWS EXCEPTION IF EMPTY (data race?)