| 169 | await self.send({"type": "websocket.send", "bytes": data}) |
| 170 | |
| 171 | async def send_json(self, data: Any, mode: str = "text") -> None: |
| 172 | if mode not in {"text", "binary"}: |
| 173 | raise RuntimeError('The "mode" argument should be "text" or "binary".') |
| 174 | text = json.dumps(data, separators=(",", ":"), ensure_ascii=False) |
| 175 | if mode == "text": |
| 176 | await self.send({"type": "websocket.send", "text": text}) |
| 177 | else: |
| 178 | await self.send({"type": "websocket.send", "bytes": text.encode("utf-8")}) |
| 179 | |
| 180 | async def close(self, code: int = 1000, reason: str | None = None) -> None: |
| 181 | await self.send({"type": "websocket.close", "code": code, "reason": reason or ""}) |