Skip to content

Commit

Permalink
[WIP] Issue #261: do not loose first websocket message due to race co…
Browse files Browse the repository at this point in the history
…ndition
  • Loading branch information
spaceone committed Jan 24, 2019
1 parent 7fea2d0 commit 9d3df4b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion circuits/web/websockets/client.py
Expand Up @@ -55,6 +55,7 @@ def __init__(self, url, channel=channel, wschannel="ws", headers=None):
self._response = None
self._pending = 0
self._wschannel = wschannel
self._codec = None

self._transport = TCPClient(channel=self.channel).register(self)
HTTP(channel=self.channel).register(self._transport)
Expand Down Expand Up @@ -109,9 +110,18 @@ def _on_response(self, response):
or response.status != 101:
self.fire(close(), self._transport)
raise NotConnected()
WebSocketCodec(
self._codec = WebSocketCodec(
data=response.body.read(), channel=self._wschannel).register(self)

@handler('read')
def _on_read(self, event, *args):
if len(args) != 1:
return
if self._codec and self._codec.parent is self:
if 'read' not in self._codec.events():
event.stop()
self.fire(event.create('read', *args))

@handler("error", priority=10)
def _on_error(self, event, error, *args, **kwargs):
# For HTTP 1.1 we leave the connection open. If the peer closes
Expand Down

0 comments on commit 9d3df4b

Please sign in to comment.