add handling for public streams

new env vars for mediamtx
switch to redis for ipc
This commit is contained in:
CEF Server 2024-08-27 14:46:44 +00:00
parent 215ffced7c
commit 6233c96e6a
5 changed files with 120 additions and 31 deletions

View file

@ -39,23 +39,9 @@ redis = Redis(host=config.REDIS_ADDR, port=6379, db=0, protocol=3)
class ErgoClient:
def __init__(self):
self.reader = None
self.writer = None
asyncio.get_running_loop().create_task(self.init())
async def init(self):
self.reader, self.writer = await asyncio.open_connection(config.ERGO_ADDR, config.ERGO_PORT)
await asyncio.create_task(self.readEvents())
asyncio.get_running_loop().create_task(self.userSub())
async def readEvents(self):
while 1:
rawLine = await self.reader.readline()
if not rawLine:
break
line = rawLine.decode("utf8").strip().split()
if line[0] == "MENTION":
await self.handleMention(line[1], line[2], line[3])
async def handleMention(self, username: str, channel: str, msgid: str):
session = SessionMaker()
@ -78,23 +64,35 @@ class ErgoClient:
await pusher.send_async(encoded)
session.close()
async def write(self, msg):
if self.writer is None:
for _ in range(30):
await asyncio.sleep(1)
if self.writer:
break
self.writer.write(msg + b"\n")
await self.writer.drain()
async def handleUserMessage(self, message):
if message:
print(f"({message['channel']}) Message Received: {message['data']}")
username = message["channel"].split(b".", 1)[1].decode("utf8")
line = message["data"].decode("utf8").split()
if line[0] == "MENTION":
await self.handleMention(username, line[1], line[2])
async def userSub(self):
async with redis.pubsub() as pubsub:
await pubsub.psubscribe("user.*")
while True:
message = await pubsub.get_message(ignore_subscribe_messages=True)
try:
await self.handleUserMessage(message)
except Exception as e:
print("Error in handling user message", e)
async def broadcastAs(self, user, *message):
await self.write(f"BROADCASTAS {user} {' '.join(message)}".encode("utf8"))
await redis.publish(f"user.{user}", f"BROADCASTAS {' '.join(message)}")
async def fullyRemoveUser(self, user):
await self.write(f"FULLYREMOVE {user}".encode("utf8"))
await redis.publish(f"user.{user}", f"FULLYREMOVE")
async def broadcastTo(self, channel, *message):
await redis.publish(f"channel.{channel}", f"BROADCASTTO {' '.join(message)}")
async def broadcastTo(self, user, *message):
await self.write(f"BROADCASTTO {user} {' '.join(message)}".encode("utf8"))
ergo = ErgoClient()