Skip to content

Commit 660dd0b

Browse files
[웹소켓] 번역
- 채팅 예시 클라이언트 사이드 까지
1 parent 3f791b9 commit 660dd0b

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

5-network/11-websocket/article.md

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,19 @@ socket.onmessage = (event) => {
188188
};
189189
```
190190

191-
## Rate limiting
191+
## 전송 제한
192192

193193
데이터 전송량이 상당한 앱을 개발하고 있다고 가정해봅시다. 그런데 우리 앱의 사용자는 모바일이나 시골같이 네트워크 속도가 느린 곳에서 앱을 사용하고 있다고 해보죠.
194194

195-
We can call `socket.send(data)` again and again. But the data will be buffered (stored) in memory and sent out only as fast as network speed allows.
195+
앱 쪽에서 `socket.send(data)`를 계속해서 호출할 순 있습니다. 하지만 이렇게 하면 데이터가 메모리에 쌓일 테고(버퍼) 네트워크 속도가 데이터를 송신하기에 충분할 때만 송신될 겁니다.
196196

197-
The `socket.bufferedAmount` property stores how many bytes remain buffered at this moment, waiting to be sent over the network.
197+
`socket.bufferedAmount` 프로퍼티는 송신 대기 중인 현재 시점에서 얼마나 많은 바이트가 메모리에 쌓여있는지 정보를 담고 있습니다.
198198

199-
We can examine it to see whether the socket is actually available for transmission.
199+
따라서 `socket.bufferedAmount` 프로퍼티 값을 확인하면 소켓을 전송에 사용할 수 있는지 아닌지를 판단할 수 있습니다.
200200

201201
```js
202-
// 100ms마다 소켓을 확인해 쌓여있는 바이트가 없는 경우에만
203-
// only if all the existing data was sent out
202+
// 100ms마다 소켓을 확인해 쌓여있는 바이트가 없는 경우에만
203+
// 데이터를 추가 전송합니다.
204204
setInterval(() => {
205205
if (socket.bufferedAmount == 0) {
206206
socket.send(moreData());
@@ -209,29 +209,28 @@ setInterval(() => {
209209
```
210210

211211

212-
## Connection close
212+
## 커넥션 닫기
213213

214-
Normally, when a party wants to close the connection (both browser and server have equal rights), they send a "connection close frame" with a numeric code and a textual reason.
214+
연결 주체(브라우저나 서버) 중 한쪽에서 커넷션 닫기(close)를 원하는 경우엔 보통 숫자로 된 코드와 문자로 된 사유가 담긴 '커넥션 종료 프레임'을 전송하게 됩니다.
215215

216-
The method for that is:
216+
메서드는 다음과 같습니다.
217217
```js
218218
socket.close([code], [reason]);
219219
```
220220

221-
- `code` is a special WebSocket closing code (optional)
222-
- `reason` is a string that describes the reason of closing (optional)
221+
- `code` -- 커넥션을 닫을 때 사용하는 특수 코드(옵션)
222+
- `reason` -- 커넥션 닫기 사유를 설명하는 문자열(옵션)
223223

224-
Then the other party in the `close` event handler gets the code and the reason, e.g.:
224+
그럼 다른 한쪽에 구현된 `close` 이벤트 핸들러에선 다음과 같이 코드와 사유를 확인할 수 있습니다.
225225

226226
```js
227-
// closing party:
227+
// 닫기를 요청한 주체:
228228
socket.close(1000, "Work complete");
229229

230-
// the other party
230+
// 다른 주체:
231231
socket.onclose = event => {
232232
// event.code === 1000
233-
// event.reason === "작업 완료"
234-
// event.wasClean === true (clean close)
233+
// event.reason === "작업 완료"
235234
};
236235
```
237236

@@ -245,11 +244,11 @@ socket.onclose = event => {
245244
- `1001` -- 연결 주체 중 한쪽이 떠남(예: 서버 셧다운, 부라우저에서 페이지 종료)
246245
- `1009` -- 메시지가 너무 커서 처리하지 못함
247246
- `1011` -- 서버 측에서 비정상적인 에러 발생
248-
- ...and so on.
247+
- ...기타 등등...
249248

250249
코드 전체 목록은 [RFC6455, §7.4.1](https://tools.ietf.org/html/rfc6455#section-7.4.1)에서 확인할 수 있습니다.
251250

252-
웹소켓 코드는 언뜻 보기엔 HTTP 코드 같아 보이지만 실제론 다릅니다. 특히 `1000`보다 작은 값은 예약 값이여서 작은 숫자를 설정하려 하면 에러가 발생합니다.
251+
웹소켓 코드는 언뜻 보기엔 HTTP 코드 같아 보이지만 실제론 다릅니다. 특히 `1000`보다 작은 값은 예약 값이여서 작은 숫자를 설정하려 하면 에러가 발생합니다.
253252

254253
```js
255254
// 사례: 커넥현 유실
@@ -261,11 +260,11 @@ socket.onclose = event => {
261260
```
262261

263262

264-
## Connection state
263+
## 커넥션 상태
265264

266-
To get connection state, additionally there's `socket.readyState` property with values:
265+
커넥션 상태를 알고 싶다면 `socket.readyState` 프로퍼티의 값을 확인하면 됩니다.
267266

268-
- **`0`** -- "CONNECTING": the connection has not yet been established,
267+
- **`0`** -- "CONNECTING": 연결 중
269268
- **`1`** -- "OPEN": 연결이 성립되고 통신 중
270269
- **`2`** -- "CLOSING": 커넥션 종료 중
271270
- **`3`** -- "CLOSED": 커넥션이 종료됨
@@ -278,35 +277,35 @@ To get connection state, additionally there's `socket.readyState` property with
278277
HTML에선 메시지를 보낼 때 사용할 `<form>`과 수신받을 메시지를 보여줄 `<div>`가 필요합니다.
279278

280279
```html
281-
<!-- 메세지-->
280+
<!-- 메시지-->
282281
<form name="publish">
283282
<input type="text" name="message">
284-
<input type="submit" value="Send">
283+
<input type="submit" value="전송">
285284
</form>
286285

287286
<!-- 수신받을 메시지가 노출될 div -->
288287
<div id="messages"></div>
289288
```
290289

291290
자바스크립트론 다음 세 가지 기능을 구현해야 합니다.
292-
1. Open the connection.
291+
1. 커넥션 생성
293292
2. form 제출 -- `socket.send(message)`를 사용해 message 전송
294293
3. 메시지 수신 처리 -- 수신한 메시지는 `div#messages`에 추가
295294

296-
Here's the code:
295+
코드는 다음과 같습니다.
297296

298297
```js
299298
let socket = new WebSocket("wss://javascript.info/article/websocket/chat/ws");
300299

301-
// send message from the form
300+
// 폼에 있는 메시지를 전송합니다.
302301
document.forms.publish.onsubmit = function() {
303302
let outgoingMessage = this.message.value;
304303

305304
socket.send(outgoingMessage);
306305
return false;
307306
};
308307

309-
// message received - show the message in div#messages
308+
// 메시지를 수신하고, 수신한 메시지를 div#messages에 보여줍니다.
310309
socket.onmessage = function(event) {
311310
let message = event.data;
312311

0 commit comments

Comments
 (0)