From 9b04034c2bbbc7cf0ddd2e1a4a0ceda90d31feb0 Mon Sep 17 00:00:00 2001 From: Vitalie Maldur Date: Sat, 23 Jul 2016 17:41:42 +0200 Subject: [PATCH] Fix for (#976): refactor websocket send_json and receive_json --- aiohttp/web_ws.py | 4 ---- aiohttp/websocket_client.py | 20 ++++++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/aiohttp/web_ws.py b/aiohttp/web_ws.py index 6c4e4a6aee1..8ec446c7d54 100644 --- a/aiohttp/web_ws.py +++ b/aiohttp/web_ws.py @@ -155,10 +155,6 @@ def send_bytes(self, data): self._writer.send(data, binary=True) def send_json(self, data, *, dumps=json.dumps): - if self._writer is None: - raise RuntimeError('Call .prepare() first') - if self._closed: - raise RuntimeError('websocket connection is closing') self.send_str(dumps(data)) @asyncio.coroutine diff --git a/aiohttp/websocket_client.py b/aiohttp/websocket_client.py index e37d87d54b9..bf52d12874b 100644 --- a/aiohttp/websocket_client.py +++ b/aiohttp/websocket_client.py @@ -90,8 +90,6 @@ def send_bytes(self, data): self._writer.send(data, binary=True) def send_json(self, data, *, dumps=json.dumps): - if self._closed: - raise RuntimeError('websocket connection is closed') self.send_str(dumps(data)) @asyncio.coroutine @@ -177,6 +175,24 @@ def receive(self): finally: self._waiting = False + @asyncio.coroutine + def receive_str(self): + msg = yield from self.receive() + if msg.tp != MsgType.text: + raise TypeError( + "Received message {}:{!r} is not str".format(msg.tp, msg.data)) + return msg.data + + @asyncio.coroutine + def receive_bytes(self): + msg = yield from self.receive() + if msg.tp != MsgType.binary: + raise TypeError( + "Received message {}:{!r} is not bytes".format(msg.tp, + msg.data)) + return msg.data + + @asyncio.coroutine def receive_json(self, *, loads=json.loads): msg = yield from self.receive() if msg.tp != MsgType.text: