#NEWLINE# def handle_message( message , topic_handlers ) : #NEWLINE# #INDENT# '\n    Deserialize and process a message from the reader.\n\n    For each message, `handler` is called with the deserialized message and a\n    single :py:class:`h.streamer.WebSocket` instance, and should return the\n    message to be sent to the client on that socket. The handler can return\n    `None`, to signify that no message should be sent, or a JSON-serializable\n    object. It is assumed that there is a 1:1 request-reply mapping between\n    incoming messages and messages to be sent out over the websockets.\n    ' #NEWLINE# data = message . payload #NEWLINE# try : #NEWLINE# #INDENT# handler = topic_handlers [ data . topic ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# raise RuntimeError ( "Don't know how to handle message from topic: {}" . format ( message . topic ) ) #NEWLINE# #UNINDENT# sockets = list ( websocket . WebSocket . instances ) #NEWLINE# for socket in sockets : #NEWLINE# #INDENT# reply = handler ( data , socket ) #NEWLINE# if ( reply is None ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( not socket . terminated ) : #NEWLINE# #INDENT# socket . send ( json . dumps ( reply ) )