forward event to local relay
This commit is contained in:
@@ -3,18 +3,18 @@ import os
|
|||||||
import websockets
|
import websockets
|
||||||
import json
|
import json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
async def relay_websockets(input_websocket, output_websocket, kinds):
|
async def relay_websockets(input_websocket, output_websocket, kinds, sub_id):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
# Wait for an event on input websocket
|
# Wait for an event on input websocket
|
||||||
event = json.loads(await input_websocket.recv())
|
event = json.loads(await input_websocket.recv())
|
||||||
try:
|
try:
|
||||||
if(event[0] == "EVENT"):
|
if(event[0] == "EVENT"):
|
||||||
# TODO: Broadcast to output websocket
|
|
||||||
print("Got event: ", event)
|
print("Got event: ", event)
|
||||||
# Output websocket broadcast will be implemented here
|
# Forward the event to output websocket
|
||||||
|
await output_websocket.send(json.dumps(["EVENT", sub_id, event[2]]))
|
||||||
elif(event[0] == "EOSE"):
|
elif(event[0] == "EOSE"):
|
||||||
print("End of stream")
|
print("End of stream")
|
||||||
|
|
||||||
@@ -31,11 +31,12 @@ async def relay_websockets(input_websocket, output_websocket, kinds):
|
|||||||
print("Connection closed, attempting to reconnect...")
|
print("Connection closed, attempting to reconnect...")
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
try:
|
try:
|
||||||
|
new_sub_id = str(uuid4())
|
||||||
async with websockets.connect(os.environ.get("INPUT_RELAY")) as new_input_websocket, \
|
async with websockets.connect(os.environ.get("INPUT_RELAY")) as new_input_websocket, \
|
||||||
websockets.connect(os.environ.get("OUTPUT_RELAY")) as new_output_websocket:
|
websockets.connect(os.environ.get("OUTPUT_RELAY")) as new_output_websocket:
|
||||||
message = '["REQ", "1337", {"kinds": '+kinds+', "limit": 10}]'
|
message = f'["REQ", "{new_sub_id}", {{"kinds": {kinds}, "limit": 10}}]'
|
||||||
await new_input_websocket.send(message)
|
await new_input_websocket.send(message)
|
||||||
await relay_websockets(new_input_websocket, new_output_websocket, kinds)
|
await relay_websockets(new_input_websocket, new_output_websocket, kinds, new_sub_id)
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
# If the reconnection attempt fails, repeat the loop and try again
|
# If the reconnection attempt fails, repeat the loop and try again
|
||||||
@@ -56,11 +57,12 @@ async def main():
|
|||||||
raise ValueError("Please set the OUTPUT_RELAY environment variable")
|
raise ValueError("Please set the OUTPUT_RELAY environment variable")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
sub_id = str(uuid4())
|
||||||
async with websockets.connect(input_url) as input_websocket, \
|
async with websockets.connect(input_url) as input_websocket, \
|
||||||
websockets.connect(output_url) as output_websocket:
|
websockets.connect(output_url) as output_websocket:
|
||||||
message = '["REQ", "1337", {"kinds": '+kinds+'}]'
|
message = f'["REQ", "{sub_id}", {{"kinds": {kinds}}}]'
|
||||||
await input_websocket.send(message)
|
await input_websocket.send(message)
|
||||||
await relay_websockets(input_websocket, output_websocket, kinds)
|
await relay_websockets(input_websocket, output_websocket, kinds, sub_id)
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
# If the initial connection attempt fails, attempt to reconnect immediately
|
# If the initial connection attempt fails, attempt to reconnect immediately
|
||||||
|
|||||||
Reference in New Issue
Block a user