Send a WebSocket frame. Server frames are not masked (RFC 6455).
(self, opcode, payload)
| 392 | # This is called for partial fragments, nothing to do here |
| 393 | |
| 394 | async def _send_frame(self, opcode, payload): |
| 395 | """Send a WebSocket frame. |
| 396 | |
| 397 | Server frames are not masked (RFC 6455). |
| 398 | """ |
| 399 | if isinstance(payload, str): |
| 400 | payload = payload.encode("utf-8") |
| 401 | |
| 402 | length = len(payload) |
| 403 | frame = bytearray() |
| 404 | |
| 405 | # First byte: FIN + opcode |
| 406 | frame.append(0x80 | opcode) |
| 407 | |
| 408 | # Second byte: length (no mask bit for server) |
| 409 | if length < 126: |
| 410 | frame.append(length) |
| 411 | elif length < 65536: |
| 412 | frame.append(126) |
| 413 | frame.extend(struct.pack("!H", length)) |
| 414 | else: |
| 415 | frame.append(127) |
| 416 | frame.extend(struct.pack("!Q", length)) |
| 417 | |
| 418 | # Payload |
| 419 | frame.extend(payload) |
| 420 | |
| 421 | self.transport.write(bytes(frame)) |
| 422 | |
| 423 | async def _send_close(self, code, reason=""): |
| 424 | """Send a close frame.""" |
no test coverage detected