You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 5-network/11-websocket/article.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,57 +1,57 @@
1
-
# WebSocket
1
+
# 웹소켓
2
2
3
3
The `WebSocket` protocol, described in the specification [RFC 6455](http://tools.ietf.org/html/rfc6455) provides a way to exchange data between browser and server via a persistent connection. The data can be passed in both directions as "packets", without breaking the connection and additional HTTP-requests.
4
4
5
-
WebSocket is especially great for services that require continuous data exchange, e.g. online games, real-time trading systems and so on.
5
+
이런 특징 때문에 웹소켓은 온라인 게임이나 주식 트레이딩 시스템같이 데이터 교환이 지속적으로 이뤄져야 하는 서비스에 아주 적합합니다.
6
6
7
7
## A simple example
8
8
9
-
To open a websocket connection, we need to create `new WebSocket` using the special protocol `ws` in the url:
9
+
웹소켓 연결을 만들려면 `new WebSocket`을 호출하면 되는데, 이때 `ws`라는 특수 프로토콜을 사용합니다.
10
10
11
11
```js
12
12
let socket =newWebSocket("*!*ws*/!*://javascript.info");
13
13
```
14
14
15
-
There's also encrypted `wss://` protocol. It's like HTTPS for websockets.
15
+
`ws`말고 `wss://`라는 프로토콜도 있는데, 두 프로토콜의 관계는 HTTP와 HTTPS의 관계와 유사하다고 보시면 됩니다.
16
16
17
-
```smart header="Always prefer `wss://`"
17
+
```smart header="항상 `wss://`를 사용합시다."
18
18
The `wss://` protocol is not only encrypted, but also more reliable.
19
19
20
20
That's because `ws://` data is not encrypted, visible for any intermediary. Old proxy servers do not know about WebSocket, they may see "strange" headers and abort the connection.
21
21
22
-
On the other hand, `wss://` is WebSocket over TLS, (same as HTTPS is HTTP over TLS), the transport security layer encrypts the data at sender and decrypts at the receiver. So data packets are passed encrypted through proxies. They can't see what's inside and let them through.
22
+
반면 `wss://`는 TSL(전송 계층 보안(Transport Layer Security))이라는 보안 계층을 통과해 전달되므로 송신자 측에서 데이터가 암호화되고, 복호화는 수신자 측에서 이뤄지게 됩니다. 따라서 데이터가 담긴 패킷은 암호화된 상태로 프락시 서버를 통과하므로 프락시 서버는 패킷 내부를 볼 수 없게 됩니다.
23
23
```
24
24
25
25
Once the socket is created, we should listen to events on it. There are totally 4 events:
26
-
- **`open`** -- connection established,
26
+
- **`open`** -- 커넥션이 제대로 만들어짐
27
27
- **`message`** -- data received,
28
28
- **`error`** -- websocket error,
29
-
- **`close`** -- connection closed.
29
+
- **`close`** -- 커넥션 종료
30
30
31
-
...And if we'd like to send something, then `socket.send(data)` will do that.
31
+
커넥션이 만들어진 상태에서 무언가를 보내고 싶으면 `socket.send(data)`를 사용하면 됩니다.
32
32
33
-
Here's an example:
33
+
예시를 살펴봅시다.
34
34
35
35
```js run
36
36
let socket = new WebSocket("wss://javascript.info/article/websocket/demo/hello");
37
37
38
38
socket.onopen = function(e) {
39
-
alert("[open] Connection established");
39
+
alert("[open] 커넥션이 만들어졌습니다.");
40
40
alert("데이터를 서버에 전송해봅시다.");
41
41
socket.send("My name is John");
42
42
};
43
43
44
44
socket.onmessage = function(event) {
45
-
alert(`[message] Data received from server: ${event.data}`);
For demo purposes, there's a small server [server.js](demo/server.js) written in Node.js, for the example above, running. It responds with "Hello from server, John", then waits 5 seconds and closes the connection.
63
+
위 예시는 데모 목적을 위해 만들어놓은 작은 Node.js 서버([server.js](demo/server.js))에서 돌아갑니다. 이 서버는 'Hello from server, John'이라는 메시지가 담긴 응답을 보내고, 5초 후 커넥션을 종료시킵니다.
64
64
65
-
So you'll see events`open` -> `message` -> `close`.
65
+
서버에 작성한 코드가 동작하면서`open` -> `message` -> `close` 순의 이벤트를 볼 수 있었던 것이죠.
66
66
67
67
That's actually it, we can talk WebSocket already. Quite simple, isn't it?
68
68
69
-
Now let's talk more in-depth.
69
+
그렇긴 하지만 실무 수준에서 웹소켓을 활용할 수 있도록 웹소켓에 대해 좀 더 자세하게 알아봅시다.
70
70
71
-
## Opening a websocket
71
+
## 웹소켓 열기
72
72
73
73
When `new WebSocket(url)` is created, it starts connecting immediately.
0 commit comments