Skip to content

Commit

Permalink
fix WS
Browse files Browse the repository at this point in the history
  • Loading branch information
makemake-kbo committed Jan 7, 2024
1 parent 69531e4 commit df3c01d
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 8 deletions.
2 changes: 1 addition & 1 deletion hrotti/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ async def handle_json_rpc(request: RPCRequest):
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
accept_ws_request(info, websocket)
await 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 @@ -73,8 +73,8 @@ def return_block(head, transaction):


def handle_request(info: BlockInfo, request: RPCRequest):
print(request)
try:
print(request.method)
match request.method:
case "eth_blockNumber":
return {"jsonrpc": "2.0", "id": request.id, "result": info.head}
Expand Down
9 changes: 5 additions & 4 deletions hrotti/websocket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from fastapi import WebSocket

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


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

0 comments on commit df3c01d

Please sign in to comment.