- 论坛徽章:
- 0
|
本帖最后由 eveson 于 2015-01-21 11:15 编辑
问题:
直接调用- self.rpc({"jsonrpc": "2.0", "method": "test", "params": [123], "id": 1})
复制代码 就没有问题
如果换成变量self.rpc(recvstr) 就会抛出异常
代码:- def test(num):
- str = {"FunId":1, "Stat":0, "param":num}
- print(str)
- return str
- global recvstr
- class MyServerProtocol(WebSocketServerProtocol):
- def __init__(self):
- self.rpc = JsonRpc()
- self.rpc['test'] = test
-
- def onConnect(self, request):
- print("Client connecting: {0}".format(request.peer))
- def onOpen(self):
- print("WebSocket connection open.")
- def onMessage(self, payload, isBinary):
- if isBinary:
- print("Binary message received: {0} bytes".format(len(payload)))
- else:
- recvstr = format(payload.decode('utf8'))
- print("Text message received:",recvstr)
- self.rpc(recvstr)
- #self.rpc({"jsonrpc": "2.0", "method": "test", "params": [123], "id": 1})
- ## echo back message verbatim
- self.sendMessage(payload, isBinary)
- def onClose(self, wasClean, code, reason):
- print("WebSocket connection closed: {0}".format(reason))
复制代码 错误:- Text message received: {"method": "test", "params": [123], "id": 1, "jsonrpc": "2.0"}
- Exception in callback WebSocketAdapterProtocol._consume.<locals>.process(<Future finished result=None>) at /usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py:94
- handle: <Handle WebSocketAdapterProtocol._consume.<locals>.process(<Future finished result=None>) at /usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py:94>
- Traceback (most recent call last):
- File "/usr/local/lib/python3.4/asyncio/events.py", line 119, in _run
- self._callback(*self._args)
- File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py", line 98, in process
- self._dataReceived(data)
- File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1328, in _dataReceived
- self.consumeData()
- File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1344, in consumeData
- while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
- File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1503, in processData
- return self.processDataHybi()
- File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1817, in processDataHybi
- fr = self.onFrameEnd()
- File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1947, in onFrameEnd
- self._onMessageEnd()
- File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py", line 154, in _onMessageEnd
- res = self.onMessageEnd()
- File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 743, in onMessageEnd
- self._onMessage(payload, self.message_is_binary)
- File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py", line 160, in _onMessage
- res = self.onMessage(payload, isBinary)
- File "server.py", line 46, in onMessage
- self.rpc(recvstr)
- File "/usr/local/lib/python3.4/site-packages/jsonrpc2-0.4.1-py3.4.egg/jsonrpc2/__init__.py", line 206, in __call__
- return resdata
- UnboundLocalError: local variable 'resdata' referenced before assignment
复制代码 |
|