MCPcopy
hub / github.com/encode/starlette / send

Method send

starlette/websockets.py:59–98  ·  starlette/websockets.py::WebSocket.send

Send ASGI websocket messages, ensuring valid state transitions.

(self, message: Message)

Source from the content-addressed store, hash-verified

57 raise RuntimeError(&class="cm">#x27;Cannot call class="st">"receive" once a disconnect message has been received.')
58
59 async def send(self, message: Message) -> None:
60 class="st">"""
61 Send ASGI websocket messages, ensuring valid state transitions.
62 class="st">"""
63 if self.application_state == WebSocketState.CONNECTING:
64 message_type = message[class="st">"type"]
65 if message_type not in {class="st">"websocket.accept", class="st">"websocket.close", class="st">"websocket.http.response.start"}:
66 raise RuntimeError(
67 &class="cm">#x27;Expected ASGI message class="st">"websocket.accept", class="st">"websocket.close" or class="st">"websocket.http.response.start", '
68 fclass="st">"but got {message_type!r}"
69 )
70 if message_type == class="st">"websocket.close":
71 self.application_state = WebSocketState.DISCONNECTED
72 elif message_type == class="st">"websocket.http.response.start":
73 self.application_state = WebSocketState.RESPONSE
74 else:
75 self.application_state = WebSocketState.CONNECTED
76 await self._send(message)
77 elif self.application_state == WebSocketState.CONNECTED:
78 message_type = message[class="st">"type"]
79 if message_type not in {class="st">"websocket.send", class="st">"websocket.close"}:
80 raise RuntimeError(
81 f&class="cm">#x27;Expected ASGI message class="st">"websocket.send" or class="st">"websocket.close", but got {message_type!r}'
82 )
83 if message_type == class="st">"websocket.close":
84 self.application_state = WebSocketState.DISCONNECTED
85 try:
86 await self._send(message)
87 except OSError:
88 self.application_state = WebSocketState.DISCONNECTED
89 raise WebSocketDisconnect(code=1006)
90 elif self.application_state == WebSocketState.RESPONSE:
91 message_type = message[class="st">"type"]
92 if message_type != class="st">"websocket.http.response.body":
93 raise RuntimeError(f&class="cm">#x27;Expected ASGI message class="st">"websocket.http.response.body", but got {message_type!r}')
94 if not message.get(class="st">"more_body", False):
95 self.application_state = WebSocketState.DISCONNECTED
96 await self._send(message)
97 else:
98 raise RuntimeError(&class="cm">#x27;Cannot call class="st">"send" once a close message has been sent.')
99
100 async def accept(
101 self,

Callers 9

acceptMethod · 0.95
send_textMethod · 0.95
send_bytesMethod · 0.95
send_jsonMethod · 0.95
closeMethod · 0.95
appFunction · 0.95
readerFunction · 0.45

Calls 3

WebSocketDisconnectClass · 0.85
_sendMethod · 0.80
getMethod · 0.45

Tested by 4

appFunction · 0.76
readerFunction · 0.36