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