Skip to content

Commit

Permalink
Fix for (aio-libs#976): refactor websocket send_json and receive_json
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalie Maldur committed Jul 23, 2016
1 parent 611a286 commit 9b04034
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 0 additions & 4 deletions aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions aiohttp/websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 9b04034

Please sign in to comment.