Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Created February 16, 2016 05:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobonobo/fdd6c8da0ea6383b58aa to your computer and use it in GitHub Desktop.
Save nobonobo/fdd6c8da0ea6383b58aa to your computer and use it in GitHub Desktop.
python3でwebsocketsでjsonrpcのサンプル
import asyncio
import websockets
from websockets.exceptions import ConnectionClosed
from jsonrpc import JSONRPCResponseManager, dispatcher
@dispatcher.add_method
def hello(*params):
return 'hello'+repr(params)
async def app(ws, path):
while True:
try:
message = await ws.recv()
response = JSONRPCResponseManager.handle(message, dispatcher)
if response is not None:
await ws.send(response.json)
except ConnectionClosed:
pass
address = '0.0.0.0'
port = 8888
if __name__ == '__main__':
start_server = websockets.serve(app, address, port)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment