diff --git a/.gitignore b/.gitignore index cfbcb6f..5ac9b2e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ Binaries/ Intermediate/ +python/ +secrets.toml diff --git a/README.md b/README.md index a5a25d4..8ca9fb9 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,23 @@ I tried Redis PubSub first, but the plugin I found was buggy. I'm trying WebSock ## 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. -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[] { diff --git a/requirements.txt b/requirements.txt index 517d109..672aac0 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/server.py b/server.py index 9248186..2de9e3c 100644 --- a/server.py +++ b/server.py @@ -4,57 +4,9 @@ import aioredis import asyncio import redis import sys +import toml 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.abc import AbcChannel @@ -64,6 +16,8 @@ CHANNELS = [ 'testing', ] +print('reading secrets', flush=True) +SECRETS = toml.load("secrets.toml") print('defining server', flush=True) @@ -91,26 +45,15 @@ async def server(websocket, path): 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] - await redis.subscribe(*channels) print('starting websocket server', flush=True) try: while True: - # Receive data from "the outside world" - #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") - + # TODO: Do we need to ping to keepalive? if not queue.empty(): msg = queue.get_nowait() # TODO THROWS EXCEPTION IF EMPTY (data race?)