Skip to content

Commit

Permalink
ws logic
Browse files Browse the repository at this point in the history
  • Loading branch information
makemake-kbo committed Jan 7, 2024
1 parent c99510f commit 69531e4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
12 changes: 4 additions & 8 deletions hrotti/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from fastapi import FastAPI, WebSocket

from .methods import RPCRequest, BlockInfo, handle_request
from .methods import BlockInfo, RPCRequest, handle_request
from .websocket_manager import accept_ws_request

app = FastAPI()

Expand All @@ -17,16 +18,11 @@

@app.post("/http")
async def handle_json_rpc(request: RPCRequest):
return handle_request(request, info)
return handle_request(info, request)


@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
try:
request = json.loads(data)
await websocket.send_text(handle_request(request.get("method"), info))
except json.JSONDecodeError:
await websocket.send_text("Error: Invalid JSON")
accept_ws_request(info, websocket)
2 changes: 1 addition & 1 deletion hrotti/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def return_block(head, transaction):
}


def handle_request(request: RPCRequest, info: BlockInfo):
def handle_request(info: BlockInfo, request: RPCRequest):
try:
print(request.method)
match request.method:
Expand Down
14 changes: 14 additions & 0 deletions hrotti/websocket_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json

from fastapi import WebSocket

from .methods import BlockInfo, handle_request


async def accept_ws_request(info: BlockInfo, websocket: WebSocket):
data = await websocket.receive_text()
try:
request = json.loads(data)
await websocket.send_text(handle_request(request.get("method"), info))
except json.JSONDecodeError:
await websocket.send_text("Error: Invalid JSON")

0 comments on commit 69531e4

Please sign in to comment.