explodinglabs/jsonrpcserver

An error occurs when using Jsonrpcserver over WebSocket with Python3.12

Closed this issue · 5 comments

8188 commented

When using jsonrpcserver over WebSocket with Python 3.10, there is no problem, but with Python 3.12, an error occurs:

...\Lib\site-packages\websockets\asyncio\server.py", line 373, in conn_handler
await self.handler(connection)
^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: main() missing 1 required positional argument: 'path'

Below is my test codes:
server.py

import asyncio
from jsonrpcserver import method, Success, Result, async_dispatch, serve
import websockets

@method
async def ping() -> Result:
    return Success("pong")

async def main(websocket, path):
    if response := await async_dispatch(await websocket.recv()):
        await websocket.send(response)

async def start_server():
    server = await websockets.serve(main, "localhost", 8765)
    await server.wait_closed()

if __name__ == "__main__":
    asyncio.run(start_server())

client.py

import asyncio
import websockets

async def send_ping():
    uri = "ws://localhost:8765"
    async with websockets.connect(uri) as websocket:
        request = '{"jsonrpc": "2.0", "method": "ping", "id": 1}'
        await websocket.send(request)
        response = await websocket.recv()
        print(f"Received: {response}")

if __name__ == "__main__":
    asyncio.run(send_ping())
bcb commented

Try the example here:
https://github.com/explodinglabs/jsonrpcserver/blob/main/examples/websockets_server.py

I just tried it with Python 3.12.6 and it worked fine.

8188 commented

Thanks a lot, @bcb. Using websockets.server.serve instead of websockets.serve resolved the bug, but still getting a DeprecationWarning. I use warnings.filterwarnings("ignore", category=DeprecationWarning) to suppress it, not elegant solution, though.

bcb commented

Is it a warning from jsonrpcserver or websockets?

8188 commented

From websockets @bcb

DeprecationWarning: websockets.server.WebSocketServerProtocol is deprecated
from websockets.server import WebSocketServerProtocol, serve
DeprecationWarning: websockets.server.serve is deprecated
from websockets.server import WebSocketServerProtocol, serve

bcb commented

It looks like the usage of the websockets library has changed in recent versions. I should update the example.