|
1 |
| -# 8.2 WebSocket |
| 1 | +# 8.2 WebSockets |
2 | 2 |
|
3 |
| -WebSocket is an important feature of HTML5, it implemented remote socket based on browsers, which allows browsers have full-duplex communication with servers. Main stream browsers like Firefox, Google Chrome and Safari have supported this feature. |
| 3 | +WebSockets are an important feature of HTML5. It implements browser based remote sockets, which allows browsers to have full-duplex communications with servers. Main stream browsers like Firefox, Google Chrome and Safari provide support for this WebSockets. |
4 | 4 |
|
5 |
| -People often use "roll poling" for instant message services before WebSocket was born, which let clients send HTTP requests in every certain period, then server returns latest data to clients. This requires clients to keep sending a lot of requests and take up a large number of bandwidth. |
| 5 | +People often used "roll polling" for instant messaging services before WebSockets were born, which allow clients to send HTTP requests periodically. The server then returns the latest data to clients. The downside to this method is that it requires clients to keep sending many requests to the server, which can consume a large amount of bandwidth. |
6 | 6 |
|
7 |
| -WebSocket uses a kind of special header to reduce handshake action between browsers and servers to only once, and create a connection. This connection will remain active, you can use JavaScript to write or read data from this the connection, as in the use of a conventional TCP socket. It solves the problem of web real-time development, and has following advantages over traditional HTTP: |
| 7 | +WebSockets use a special kind of header that reduces the number of handshakes required between browser and server to only one, for establishing a connection. This connection will remain active throughout its lifetime, and you can use JavaScript to write or read data from this connection, as in the case of a conventional TCP sockets. It solves many of the headache involved with real-time web development, and has the following advantages over traditional HTTP: |
8 | 8 |
|
9 |
| -- Only one TCP connection for a singe web client. |
| 9 | +- Only one TCP connection for a single web client. |
10 | 10 | - WebSocket servers can push data to web clients.
|
11 |
| -- More lightweight header to reduce data transmission. |
| 11 | +- Lightweight header to reduce data transmission overhead. |
12 | 12 |
|
13 |
| -WebSocket URL starts with ws:// or wss://(SSL). The following picture shows the communication process of WebSocket, where a particular HTTP header was sent to server for handshake, then servers or clients are able to send or receive data through JavaScript according to some kind of socket, this socket can be used by the event handler to receive data asynchronously. |
| 13 | +WebSocket URLs begin with ws:// or wss://(SSL). The following figure shows the communication process of WebSockets. A particular HTTP header is sent to the server as part of the handshaking protocol and the connection is established. Then, servers or clients are able to send or receive data through JavaScript via WebSocket. This socket can then be used by an event handler to receive data asynchronously. |
14 | 14 |
|
15 | 15 | 
|
16 | 16 |
|
17 |
| -Figure 8.2 WebSocket principle. |
| 17 | +Figure 8.2 WebSocket principl |
18 | 18 |
|
19 |
| -## WebSocket principle |
| 19 | +## WebSocket principles |
20 | 20 |
|
21 |
| -WebSocket protocol is quite simple, after the adoption of the first handshake, the connection is established successfully. Subsequent communication data are all begin with "\x00" and ends with "\xFF". Clients will not see these two parts because WebSocket will break off both ends and gives raw data automatically. |
| 21 | +The WebSocket protocol is actually quite simple. After successfully completing the initial handshake, a connection is established. Subsequent data communications will all begin with "\x00" and end with "\xFF". This prefix and suffix will be visible to clients because the WebSocket will break off both end, yielding the raw data automatically. |
22 | 22 |
|
23 |
| -WebSocket connection are requested by browsers and responded by servers, then the connection is established, this process is often called "handshake". |
| 23 | +WebSocket connections are requested by browsers and responded to by servers, after which the connection is established. This process is often called "handshaking". |
24 | 24 |
|
25 |
| -Consider the following requests and feedback: |
| 25 | +Consider the following requests and responses: |
26 | 26 |
|
27 | 27 | 
|
28 | 28 |
|
29 | 29 | Figure 8.3 WebSocket request and response.
|
30 | 30 |
|
31 |
| -"Sec-WebSocket-key" is generated randomly, as you may guess, this is encoded by base64. Servers need to append this key to a fixed string after accepted: |
| 31 | +"Sec-WebSocket-key" is generated randomly, as you may have already guessed, and it's base64 encoded. Servers need to append this key to a fixed string after accepting a request: |
32 | 32 |
|
33 | 33 | 258EAFA5-E914-47DA-95CA-C5AB0DC85B11
|
34 | 34 |
|
35 | 35 | Suppose we have `f7cb4ezEAl6C3wRaU6JORA==`, then we have:
|
36 | 36 |
|
37 | 37 | f7cb4ezEAl6C3wRaU6JORA==258EAFA5-E914-47DA-95CA-C5AB0DC85B11
|
38 | 38 |
|
39 |
| -Use sha1 to compute binary value and use base64 to encode it, then we have: |
| 39 | +Use sha1 to compute the binary value and use base64 to encode it. We will then we have: |
40 | 40 |
|
41 | 41 | rE91AJhfC+6JdVcVXOGJEADEJdQ=
|
42 | 42 |
|
43 |
| -Use this as value of `Sec-WebSocket-Accept` for respond header. |
| 43 | +Use this as the value of the `Sec-WebSocket-Accept` response header. |
44 | 44 |
|
45 | 45 | ## WebSocket in Go
|
46 | 46 |
|
47 |
| -Go standard library does not support WebSocket, but package `websocket`, which is the sub-package of `go.net` and maintained by official support it. |
| 47 | +The Go standard library does not support WebSockets. However the `websocket` package, which is a sub-package of `go.net` does, and is officially maintained and supported. |
48 | 48 |
|
49 | 49 | Use `go get` to install this package:
|
50 | 50 |
|
51 | 51 | go get code.google.com/p/go.net/websocket
|
52 | 52 |
|
53 |
| -WebSocket has client and server sides, let's see a simple example: user input information, client sends content to server through WebSocket; server pushes information back up client. |
| 53 | +WebSockets have both client and server sides. Let's see a simple example where a user inputs some information on the client side and sends it to the server through a WebSocket, followed by the server pushing information back to the client. |
54 | 54 |
|
55 | 55 | Client code:
|
56 | 56 |
|
@@ -95,12 +95,12 @@ Client code:
|
95 | 95 | </body>
|
96 | 96 | </html>
|
97 | 97 |
|
98 |
| -As you can see, JavaScript is very easy to write in client side, and use corresponding function establish a connection. Event `onopen` triggered after handshake to tell client that connection was created successfully. Client bindings four events: |
| 98 | +As you can see, it's very easy to use the client side JavaScript functions to establish a connection. The `onopen` event gets triggered after successfully completing the aforementioned handshaking process. It tells the client that the connection has been created successfully. Clients attempting to open a connection typically bind to four events: |
99 | 99 |
|
100 |
| -- 1)onopen: triggered after connection was established. |
101 |
| -- 2)onmessage: triggered after received message. |
102 |
| -- 3)onerror: triggered after error occurred. |
103 |
| -- 4)onclose: triggered after connection closed. |
| 100 | +- 1)onopen: triggered after connection has been established. |
| 101 | +- 2)onmessage: triggered after receiving a message. |
| 102 | +- 3)onerror: triggered after an error has occurred.. |
| 103 | +- 4)onclose: triggered after the connection has closed. |
104 | 104 |
|
105 | 105 | Server code:
|
106 | 106 |
|
@@ -144,13 +144,13 @@ Server code:
|
144 | 144 | }
|
145 | 145 | }
|
146 | 146 |
|
147 |
| -When client `Send` user input information, server `Receive` it, and use `Send` to return feedback. |
| 147 | +When a client `Send`s user input information, the server `Receive`s it, and uses `Send` once again to return a response. |
148 | 148 |
|
149 | 149 | 
|
150 | 150 |
|
151 | 151 | Figure 8.4 WebSocket server received information.
|
152 | 152 |
|
153 |
| -Through the example above we see that the client and server side implementation of WebSocket are very convenient. We can use package `net` directly in Go. Now with rapid develop of HTML5, I think WebSocket will be much more important in web development, we need to reserve this knowledge. |
| 153 | +Through the example above, we can see that the client and server side implementation of WebSockets is very convenient. We can use the `net` package directly in Go. With the rapid development of HTML5, I think that WebSockets will take on a much more important role in modern day web development; we should all be at least a little bit familiar with them. |
154 | 154 |
|
155 | 155 | ## Links
|
156 | 156 |
|
|
0 commit comments