| 131 | |
| 132 | @asyncio.coroutine |
| 133 | def receive(self): |
| 134 | if self._waiting: |
| 135 | raise RuntimeError('Concurrent call to receive() is not allowed') |
| 136 | |
| 137 | self._waiting = True |
| 138 | try: |
| 139 | while True: |
| 140 | if self._closed: |
| 141 | return closedMessage |
| 142 | |
| 143 | try: |
| 144 | msg = yield from self._reader.read() |
| 145 | except (asyncio.CancelledError, asyncio.TimeoutError): |
| 146 | raise |
| 147 | except WebSocketError as exc: |
| 148 | self._close_code = exc.code |
| 149 | yield from self.close(code=exc.code) |
| 150 | return Message(MsgType.error, exc, None) |
| 151 | except Exception as exc: |
| 152 | self._exception = exc |
| 153 | self._closing = True |
| 154 | self._close_code = 1006 |
| 155 | yield from self.close() |
| 156 | return Message(MsgType.error, exc, None) |
| 157 | |
| 158 | if msg.tp == MsgType.close: |
| 159 | self._closing = True |
| 160 | self._close_code = msg.data |
| 161 | if not self._closed and self._autoclose: |
| 162 | yield from self.close() |
| 163 | return msg |
| 164 | elif not self._closed: |
| 165 | if msg.tp == MsgType.ping and self._autoping: |
| 166 | self._writer.pong(msg.data) |
| 167 | elif msg.tp == MsgType.pong and self._autoping: |
| 168 | continue |
| 169 | else: |
| 170 | return msg |
| 171 | finally: |
| 172 | self._waiting = False |
| 173 | |
| 174 | if PY_35: |
| 175 | @asyncio.coroutine |