def testControlFrames(self): s = self.parser() c = self.parser(kind=1) # chunk = s.close(1001) frame = c.decode(chunk) self.assertTrue(frame.final) self.assertEqual(frame.opcode, 8) code, reason = parse_close(frame.body) self.assertEqual(code, 1001) # chunk = s.ping("Hello") frame = c.decode(chunk) self.assertTrue(frame.final) self.assertEqual(frame.opcode, 9) self.assertEqual(i2b((0x89, 0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F)), chunk) self.assertEqual(frame.body, b"Hello") self.assertRaises(ProtocolError, s.ping, self.bdata) # chunk = s.pong("Hello") frame = c.decode(chunk) self.assertTrue(frame.final) self.assertEqual(frame.opcode, 10) self.assertEqual(frame.body, b"Hello") self.assertRaises(ProtocolError, s.pong, self.bdata)
def testControlFrames(self): s = self.parser() c = self.parser(kind=1) # chunk = s.close(1001) frame = c.decode(chunk) self.assertTrue(frame.final) self.assertEqual(frame.opcode, 8) code, reason = parse_close(frame.body) self.assertEqual(code, 1001) # chunk = s.ping('Hello') frame = c.decode(chunk) self.assertTrue(frame.final) self.assertEqual(frame.opcode, 9) self.assertEqual(i2b((0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f)), chunk) self.assertEqual(frame.body, b'Hello') self.assertRaises(ProtocolError, s.ping, self.bdata) # chunk = s.pong('Hello') frame = c.decode(chunk) self.assertTrue(frame.final) self.assertEqual(frame.opcode, 10) self.assertEqual(frame.body, b'Hello') self.assertRaises(ProtocolError, s.pong, self.bdata)
def data_received(self, data): frame = self.parser.decode(data) while frame: if frame.is_close: try: self.close_reason = parse_close(frame.body) finally: self._connection.close() break if frame.is_message: self._on(self.handler.on_message, frame) elif frame.is_bytes: self._on(self.handler.on_bytes, frame) elif frame.is_ping: self._on(self.handler.on_ping, frame) elif frame.is_pong: self._on(self.handler.on_pong, frame) frame = self.parser.decode()