You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4.8 KiB

*️⃣

👆 💪 ⚙️ *️⃣ ⏮️ FastAPI.

WebSockets

🥇 👆 💪 WebSockets:

$ pip install websockets

---> 100%

*️⃣ 👩‍💻

🏭

👆 🏭 ⚙️, 👆 🎲 ✔️ 🕸 ✍ ⏮️ 🏛 🛠️ 💖 😥, Vue.js ⚖️ 📐.

& 🔗 ⚙️ *️⃣ ⏮️ 👆 👩‍💻 👆 🔜 🎲 ⚙️ 👆 🕸 🚙.

⚖️ 👆 💪 ✔️ 🇦🇸 📱 🈸 👈 🔗 ⏮️ 👆 *️⃣ 👩‍💻 🔗, 🇦🇸 📟.

⚖️ 👆 5️⃣📆 ✔️ 🙆 🎏 🌌 🔗 ⏮️ *️⃣ 🔗.


👉 🖼, 👥 🔜 ⚙️ 📶 🙅 🕸 📄 ⏮️ 🕸, 🌐 🔘 📏 🎻.

👉, ↗️, 🚫 ⚖ & 👆 🚫🔜 ⚙️ 🏭.

🏭 👆 🔜 ✔️ 1️⃣ 🎛 🔛.

🙅 🌌 🎯 🔛 💽-🚄 *️⃣ & ✔️ 👷 🖼:

{!../../../docs_src/websockets/tutorial001.py!}

websocket

👆 FastAPI 🈸, ✍ websocket:

{!../../../docs_src/websockets/tutorial001.py!}

!!! note "📡 ℹ" 👆 💪 ⚙️ from starlette.websockets import WebSocket.

**FastAPI** 🚚 🎏 `WebSocket` 🔗 🏪 👆, 👩‍💻. ✋️ ⚫️ 👟 🔗 ⚪️➡️ 💃.

📧 & 📨 📧

👆 *️⃣ 🛣 👆 💪 await 📧 & 📨 📧.

{!../../../docs_src/websockets/tutorial001.py!}

👆 💪 📨 & 📨 💱, ✍, & 🎻 💽.

🔄

🚥 👆 📁 📛 main.py, 🏃 👆 🈸 ⏮️:

$ uvicorn main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

📂 👆 🖥 http://127.0.0.1:8000.

👆 🔜 👀 🙅 📃 💖:

👆 💪 🆎 📧 🔢 📦, & 📨 👫:

& 👆 FastAPI 🈸 ⏮️ *️⃣ 🔜 📨 🔙:

👆 💪 📨 (& 📨) 📚 📧:

& 🌐 👫 🔜 ⚙️ 🎏 *️⃣ 🔗.

⚙️ Depends & 🎏

*️⃣ 🔗 👆 💪 🗄 ➡️ fastapi & ⚙️:

  • Depends
  • Security
  • Cookie
  • Header
  • Path
  • Query

👫 👷 🎏 🌌 🎏 FastAPI 🔗/🛠️:

{!../../../docs_src/websockets/tutorial002.py!}

!!! info 👉 *️⃣ 🚫 🤙🔑 🤚 HTTPException, ↩️ 👥 🤚 WebSocketException.

👆 💪 ⚙️ 📪 📟 ⚪️➡️ <a href="https://tools.ietf.org/html/rfc6455#section-7.4.1" class="external-link" target="_blank">☑ 📟 🔬 🔧</a>.

🔄 *️⃣ ⏮️ 🔗

🚥 👆 📁 📛 main.py, 🏃 👆 🈸 ⏮️:

$ uvicorn main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

📂 👆 🖥 http://127.0.0.1:8000.

📤 👆 💪 ⚒:

  • "🏬 🆔", ⚙️ ➡.
  • "🤝" ⚙️ 🔢 🔢.

!!! tip 👀 👈 🔢 token 🔜 🍵 🔗.

⏮️ 👈 👆 💪 🔗 *️⃣ & ⤴️ 📨 & 📨 📧:

🚚 🔀 & 💗 👩‍💻

🕐 *️⃣ 🔗 📪, await websocket.receive_text() 🔜 🤚 WebSocketDisconnect ⚠, 👆 💪 ⤴️ & 🍵 💖 👉 🖼.

{!../../../docs_src/websockets/tutorial003.py!}

🔄 👅:

  • 📂 📱 ⏮️ 📚 🖥 📑.
  • 📧 ➡️ 👫.
  • ⤴️ 🔐 1️⃣ 📑.

👈 🔜 🤚 WebSocketDisconnect ⚠, & 🌐 🎏 👩‍💻 🔜 📨 📧 💖:

Client #1596980209979 left the chat

!!! tip 📱 🔛 & 🙅 🖼 🎦 🍵 & 📻 📧 📚 *️⃣ 🔗.

✋️ ✔️ 🤯 👈, 🌐 🍵 💾, 👁 📇, ⚫️ 🔜 🕴 👷 ⏪ 🛠️ 🏃, &amp; 🔜 🕴 👷 ⏮️ 👁 🛠️.

🚥 👆 💪 🕳 ⏩ 🛠️ ⏮️ FastAPI ✋️ 👈 🌖 🏋️, 🐕‍🦺 ✳, ✳ ⚖️ 🎏, ✅ <a href="https://github.com/encode/broadcaster" class="external-link" target="_blank">🗜/📻</a>.

🌅

💡 🌅 🔃 🎛, 💃 🧾: