Browse Source

๐ŸŒ Add Korean translation for `docs/ko/docs/advanced/websockets.md` (#12991)

pull/13004/head
LKY 4 months ago
committed by GitHub
parent
commit
478644086e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 186
      docs/ko/docs/advanced/websockets.md

186
docs/ko/docs/advanced/websockets.md

@ -0,0 +1,186 @@
# WebSockets
์—ฌ๋Ÿฌ๋ถ„์€ **FastAPI**์—์„œ <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API" class="external-link" target="_blank">WebSockets</a>๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
## `WebSockets` ์„ค์น˜
[๊ฐ€์ƒ ํ™˜๊ฒฝ](../virtual-environments.md){.internal-link target=_blank)๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ™œ์„ฑํ™”ํ•œ ๋‹ค์Œ, `websockets`๋ฅผ ์„ค์น˜ํ•˜์„ธ์š”:
<div class="termy">
```console
$ pip install websockets
---> 100%
```
</div>
## WebSockets ํด๋ผ์ด์–ธํŠธ
### ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ
์—ฌ๋Ÿฌ๋ถ„์˜ ํ”„๋กœ๋•์…˜ ์‹œ์Šคํ…œ์—์„œ๋Š” React, Vue.js ๋˜๋Š” Angular์™€ ๊ฐ™์€ ์ตœ์‹  ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ์ƒ์„ฑ๋œ ํ”„๋ŸฐํŠธ์—”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค.
๋ฐฑ์—”๋“œ์™€ WebSockets์„ ์‚ฌ์šฉํ•ด ํ†ต์‹ ํ•˜๋ ค๋ฉด ์•„๋งˆ๋„ ํ”„๋ŸฐํŠธ์—”๋“œ์˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๋˜๋Š” ๋„ค์ดํ‹ฐ๋ธŒ ์ฝ”๋“œ๋กœ WebSocket ๋ฐฑ์—”๋“œ์™€ ์ง์ ‘ ํ†ต์‹ ํ•˜๋Š” ๋„ค์ดํ‹ฐ๋ธŒ ๋ชจ๋ฐ”์ผ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ˜น์€ WebSocket ์—”๋“œํฌ์ธํŠธ์™€ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
---
ํ•˜์ง€๋งŒ ์ด๋ฒˆ ์˜ˆ์ œ์—์„œ๋Š” ์ผ๋ถ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํฌํ•จํ•œ ๊ฐ„๋‹จํ•œ HTML ๋ฌธ์„œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ฒƒ์„ ๊ธด ๋ฌธ์ž์—ด ์•ˆ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
๋ฌผ๋ก , ์ด๋Š” ์ตœ์ ์˜ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋ฉฐ ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” ์œ„์—์„œ ์„ค๋ช…ํ•œ ์˜ต์…˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ์ด๋Š” WebSockets์˜ ์„œ๋ฒ„ ์ธก์— ์ง‘์ค‘ํ•˜๊ณ  ๋™์ž‘ํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค:
{* ../../docs_src/websockets/tutorial001.py hl[2,6:38,41:43] *}
## `websocket` ์ƒ์„ฑํ•˜๊ธฐ
**FastAPI** ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ `websocket`์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค:
{* ../../docs_src/websockets/tutorial001.py hl[1,46:47] *}
/// note | ๊ธฐ์ˆ ์  ์„ธ๋ถ€์‚ฌํ•ญ
`from starlette.websockets import WebSocket`์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
**FastAPI**๋Š” ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํŽธ์˜๋ฅผ ์œ„ํ•ด ๋™์ผํ•œ `WebSocket`์„ ์ง์ ‘ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Š” Starlette์—์„œ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
///
## ๋ฉ”์‹œ์ง€๋ฅผ ๋Œ€๊ธฐํ•˜๊ณ  ์ „์†กํ•˜๊ธฐ
WebSocket ๊ฒฝ๋กœ์—์„œ ๋ฉ”์‹œ์ง€๋ฅผ ๋Œ€๊ธฐ(`await`)ํ•˜๊ณ  ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
{* ../../docs_src/websockets/tutorial001.py hl[48:52] *}
์—ฌ๋Ÿฌ๋ถ„์€ ์ด์ง„ ๋ฐ์ดํ„ฐ, ํ…์ŠคํŠธ, JSON ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ  ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
## ์‹œ๋„ํ•ด๋ณด๊ธฐ
ํŒŒ์ผ ์ด๋ฆ„์ด `main.py`๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค:
<div class="termy">
```console
$ fastapi dev main.py
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```
</div>
๋ธŒ๋ผ์šฐ์ €์—์„œ <a href="http://127.0.0.1:8000" class="external-link" target="_blank">http://127.0.0.1:8000</a>์„ ์—ด์–ด๋ณด์„ธ์š”.
๊ฐ„๋‹จํ•œ ํŽ˜์ด์ง€๊ฐ€ ๋‚˜ํƒ€๋‚  ๊ฒƒ์ž…๋‹ˆ๋‹ค:
<img src="/img/tutorial/websockets/image01.png">
์ž…๋ ฅ์ฐฝ์— ๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
<img src="/img/tutorial/websockets/image02.png">
**FastAPI** WebSocket ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด ์‘๋‹ต์„ ๋Œ๋ ค์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค:
<img src="/img/tutorial/websockets/image03.png">
์—ฌ๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†ก(๊ทธ๋ฆฌ๊ณ  ์ˆ˜์‹ )ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
<img src="/img/tutorial/websockets/image04.png">
๋ชจ๋“  ๋ฉ”์‹œ์ง€๋Š” ๋™์ผํ•œ WebSocket ์—ฐ๊ฒฐ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
## `Depends` ๋ฐ ๊ธฐํƒ€ ์‚ฌ์šฉํ•˜๊ธฐ
WebSocket ์—”๋“œํฌ์ธํŠธ์—์„œ `fastapi`์—์„œ ๋‹ค์Œ์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
* `Depends`
* `Security`
* `Cookie`
* `Header`
* `Path`
* `Query`
์ด๋“ค์€ ๋‹ค๋ฅธ FastAPI ์—”๋“œํฌ์ธํŠธ/*๊ฒฝ๋กœ ์ž‘๋™*๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค:
{* ../../docs_src/websockets/tutorial002_an_py310.py hl[68:69,82] *}
/// info | ์ •๋ณด
WebSocket์—์„œ๋Š” `HTTPException`์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  `WebSocketException`์„ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.
๋ช…์„ธ์„œ์— ์ •์˜๋œ <a href="https://tools.ietf.org/html/rfc6455#section-7.4.1" class="external-link" target="_blank">์œ ํšจํ•œ ์ฝ”๋“œ</a>๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ข…๋ฃŒ ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
///
### ์ข…์†์„ฑ์„ ๊ฐ€์ง„ WebSockets ํ…Œ์ŠคํŠธ
ํŒŒ์ผ ์ด๋ฆ„์ด `main.py`๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค:
<div class="termy">
```console
$ fastapi dev main.py
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```
</div>
๋ธŒ๋ผ์šฐ์ €์—์„œ <a href="http://127.0.0.1:8000" class="external-link" target="_blank">http://127.0.0.1:8000</a>์„ ์—ด์–ด๋ณด์„ธ์š”.
๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
* ๊ฒฝ๋กœ์— ์‚ฌ์šฉ๋œ "Item ID".
* ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ๋œ "Token".
/// tip | ํŒ
์ฟผ๋ฆฌ `token`์€ ์ข…์†์„ฑ์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
///
์ด์ œ WebSocket์— ์—ฐ๊ฒฐํ•˜๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†ก ๋ฐ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
<img src="/img/tutorial/websockets/image05.png">
## ์—ฐ๊ฒฐ ํ•ด์ œ ๋ฐ ๋‹ค์ค‘ ํด๋ผ์ด์–ธํŠธ ์ฒ˜๋ฆฌ
WebSocket ์—ฐ๊ฒฐ์ด ๋‹ซํžˆ๋ฉด, `await websocket.receive_text()`๊ฐ€ `WebSocketDisconnect` ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋ฅผ ์žก์•„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
{* ../../docs_src/websockets/tutorial003_py39.py hl[79:81] *}
ํ…Œ์ŠคํŠธํ•ด๋ณด๊ธฐ:
* ์—ฌ๋Ÿฌ ๋ธŒ๋ผ์šฐ์ € ํƒญ์—์„œ ์•ฑ์„ ์—ฝ๋‹ˆ๋‹ค.
* ๊ฐ ํƒญ์—์„œ ๋ฉ”์‹œ์ง€๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
* ํ•œ ํƒญ์„ ๋‹ซ์•„๋ณด์„ธ์š”.
`WebSocketDisconnect` ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ, ๋‹ค๋ฅธ ๋ชจ๋“  ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค:
```
Client #1596980209979 left the chat
```
/// tip | ํŒ
์œ„ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์€ ์—ฌ๋Ÿฌ WebSocket ์—ฐ๊ฒฐ์— ๋ฉ”์‹œ์ง€๋ฅผ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ๋ชจ๋“  ๊ฒƒ์„ ๋ฉ”๋ชจ๋ฆฌ์˜ ๋‹จ์ผ ๋ฆฌ์ŠคํŠธ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ, ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰ ์ค‘์ธ ๋™์•ˆ๋งŒ ๋™์ž‘ํ•˜๋ฉฐ ๋‹จ์ผ ํ”„๋กœ์„ธ์Šค์—์„œ๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
FastAPI์™€ ์‰ฝ๊ฒŒ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด์„œ ๋” ๊ฒฌ๊ณ ํ•˜๊ณ  Redis, PostgreSQL ๋“ฑ์„ ์ง€์›ํ•˜๋Š” ๋„๊ตฌ๋ฅผ ์ฐพ๊ณ  ์žˆ๋‹ค๋ฉด, <a href="https://github.com/encode/broadcaster" class="external-link" target="_blank">encode/broadcaster</a>๋ฅผ ํ™•์ธํ•˜์„ธ์š”.
///
## ์ถ”๊ฐ€ ์ •๋ณด
๋‹ค์Œ ์˜ต์…˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ๋ณด๋ ค๋ฉด Starlette์˜ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”:
* <a href="https://www.starlette.io/websockets/" class="external-link" target="_blank">`WebSocket` ํด๋ž˜์Šค</a>.
* <a href="https://www.starlette.io/endpoints/#websocketendpoint" class="external-link" target="_blank">ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ WebSocket ์ฒ˜๋ฆฌ</a>.
Loadingโ€ฆ
Cancel
Save