WebSockets¶
在定义 WebSockets 时,通常会声明一个类型为 WebSocket
的参数,通过它你可以从客户端读取数据并向其发送数据。
它由 Starlette 直接提供,但你也可以从 fastapi
导入它:
from fastapi import WebSocket
Tip
当你想要定义一个既兼容 HTTP 又兼容 WebSockets 的依赖项时,你可以定义一个接受 HTTPConnection
而不是 Request
或 WebSocket
的参数。
fastapi.WebSocket
¶
WebSocket(scope, receive, send)
Bases: HTTPConnection
PARAMETER | DESCRIPTION |
---|---|
scope |
TYPE:
|
receive |
TYPE:
|
send |
TYPE:
|
Source code in starlette/websockets.py
26 27 28 29 30 31 32 |
|
url_for
¶
url_for(name, /, **path_params)
PARAMETER | DESCRIPTION |
---|---|
name |
TYPE:
|
**path_params |
TYPE:
|
Source code in starlette/requests.py
177 178 179 180 |
|
receive
async
¶
receive()
Receive ASGI websocket messages, ensuring valid state transitions.
Source code in starlette/websockets.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
send
async
¶
send(message)
Send ASGI websocket messages, ensuring valid state transitions.
PARAMETER | DESCRIPTION |
---|---|
message |
TYPE:
|
Source code in starlette/websockets.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
accept
async
¶
accept(subprotocol=None, headers=None)
PARAMETER | DESCRIPTION |
---|---|
subprotocol |
TYPE:
|
headers |
TYPE:
|
Source code in starlette/websockets.py
99 100 101 102 103 104 105 106 107 108 109 |
|
receive_text
async
¶
receive_text()
Source code in starlette/websockets.py
115 116 117 118 119 120 |
|
receive_bytes
async
¶
receive_bytes()
Source code in starlette/websockets.py
122 123 124 125 126 127 |
|
receive_json
async
¶
receive_json(mode='text')
PARAMETER | DESCRIPTION |
---|---|
mode |
TYPE:
|
Source code in starlette/websockets.py
129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
iter_text
async
¶
iter_text()
Source code in starlette/websockets.py
143 144 145 146 147 148 |
|
iter_bytes
async
¶
iter_bytes()
Source code in starlette/websockets.py
150 151 152 153 154 155 |
|
iter_json
async
¶
iter_json()
Source code in starlette/websockets.py
157 158 159 160 161 162 |
|
send_text
async
¶
send_text(data)
PARAMETER | DESCRIPTION |
---|---|
data |
TYPE:
|
Source code in starlette/websockets.py
164 165 |
|
send_bytes
async
¶
send_bytes(data)
PARAMETER | DESCRIPTION |
---|---|
data |
TYPE:
|
Source code in starlette/websockets.py
167 168 |
|
send_json
async
¶
send_json(data, mode='text')
PARAMETER | DESCRIPTION |
---|---|
data |
TYPE:
|
mode |
TYPE:
|
Source code in starlette/websockets.py
170 171 172 173 174 175 176 177 |
|
close
async
¶
close(code=1000, reason=None)
PARAMETER | DESCRIPTION |
---|---|
code |
TYPE:
|
reason |
TYPE:
|
Source code in starlette/websockets.py
179 180 |
|
当客户端断开连接时,会引发一个 WebSocketDisconnect
异常,你可以捕获它。
你可以直接从 fastapi
导入它:
from fastapi import WebSocketDisconnect
fastapi.WebSocketDisconnect
¶
WebSocketDisconnect(code=1000, reason=None)
Bases: Exception
PARAMETER | DESCRIPTION |
---|---|
code |
TYPE:
|
reason |
TYPE:
|
Source code in starlette/websockets.py
20 21 22 |
|
WebSockets - 附加类¶
用于处理 WebSockets 的附加类。
由 Starlette 直接提供,但你也可以从 fastapi
导入它:
from fastapi.websockets import WebSocketDisconnect, WebSocketState
fastapi.websockets.WebSocketDisconnect
¶
WebSocketDisconnect(code=1000, reason=None)
Bases: Exception
PARAMETER | DESCRIPTION |
---|---|
code |
TYPE:
|
reason |
TYPE:
|
Source code in starlette/websockets.py
20 21 22 |
|