WebSocket, Eng grammar
This commit is contained in:
parent
7964b11b8f
commit
b30b705d94
1 changed files with 15 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
|||
# WebSocket
|
||||
|
||||
The `WebSocket` protocol, described in the specification [RFC 6455](https://datatracker.ietf.org/doc/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.
|
||||
The `WebSocket` protocol, described in the specification [RFC 6455](https://datatracker.ietf.org/doc/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 the need of additional HTTP-requests.
|
||||
|
||||
WebSocket is especially great for services that require continuous data exchange, e.g. online games, real-time trading systems and so on.
|
||||
|
||||
|
@ -19,7 +19,7 @@ The `wss://` protocol is not only encrypted, but also more reliable.
|
|||
|
||||
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.
|
||||
|
||||
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.
|
||||
On the other hand, `wss://` is WebSocket over TLS, (same as HTTPS is HTTP over TLS), the transport security layer encrypts the data at the sender and decrypts it at the receiver. So data packets are passed encrypted through proxies. They can't see what's inside and let them through.
|
||||
```
|
||||
|
||||
Once the socket is created, we should listen to events on it. There are totally 4 events:
|
||||
|
@ -72,11 +72,11 @@ Now let's talk more in-depth.
|
|||
|
||||
When `new WebSocket(url)` is created, it starts connecting immediately.
|
||||
|
||||
During the connection the browser (using headers) asks the server: "Do you support Websocket?" And if the server replies "yes", then the talk continues in WebSocket protocol, which is not HTTP at all.
|
||||
During the connection, the browser (using headers) asks the server: "Do you support Websocket?" And if the server replies "yes", then the talk continues in WebSocket protocol, which is not HTTP at all.
|
||||
|
||||

|
||||
|
||||
Here's an example of browser headers for request made by `new WebSocket("wss://javascript.info/chat")`.
|
||||
Here's an example of browser headers for a request made by `new WebSocket("wss://javascript.info/chat")`.
|
||||
|
||||
```
|
||||
GET /chat
|
||||
|
@ -88,7 +88,7 @@ Sec-WebSocket-Key: Iv8io/9s+lYFgZWcXczP8Q==
|
|||
Sec-WebSocket-Version: 13
|
||||
```
|
||||
|
||||
- `Origin` -- the origin of the client page, e.g. `https://javascript.info`. WebSocket objects are cross-origin by nature. There are no special headers or other limitations. Old servers are unable to handle WebSocket anyway, so there are no compatibility issues. But `Origin` header is important, as it allows the server to decide whether or not to talk WebSocket with this website.
|
||||
- `Origin` -- the origin of the client page, e.g. `https://javascript.info`. WebSocket objects are cross-origin by nature. There are no special headers or other limitations. Old servers are unable to handle WebSocket anyway, so there are no compatibility issues. But the `Origin` header is important, as it allows the server to decide whether or not to talk WebSocket with this website.
|
||||
- `Connection: Upgrade` -- signals that the client would like to change the protocol.
|
||||
- `Upgrade: websocket` -- the requested protocol is "websocket".
|
||||
- `Sec-WebSocket-Key` -- a random browser-generated key for security.
|
||||
|
@ -109,7 +109,7 @@ Sec-WebSocket-Accept: hsBlbuDTkk24srzEOTBUlZAlC2g=
|
|||
|
||||
Here `Sec-WebSocket-Accept` is `Sec-WebSocket-Key`, recoded using a special algorithm. The browser uses it to make sure that the response corresponds to the request.
|
||||
|
||||
Afterwards, the data is transfered using WebSocket protocol, we'll see its structure ("frames") soon. And that's not HTTP at all.
|
||||
Afterwards, the data is transferred using the WebSocket protocol, we'll see its structure ("frames") soon. And that's not HTTP at all.
|
||||
|
||||
### Extensions and subprotocols
|
||||
|
||||
|
@ -117,9 +117,9 @@ There may be additional headers `Sec-WebSocket-Extensions` and `Sec-WebSocket-Pr
|
|||
|
||||
For instance:
|
||||
|
||||
- `Sec-WebSocket-Extensions: deflate-frame` means that the browser supports data compression. An extension is something related to transferring the data, functionality that extends WebSocket protocol. The header `Sec-WebSocket-Extensions` is sent automatically by the browser, with the list of all extensions it supports.
|
||||
- `Sec-WebSocket-Extensions: deflate-frame` means that the browser supports data compression. An extension is something related to transferring the data, functionality that extends the WebSocket protocol. The header `Sec-WebSocket-Extensions` is sent automatically by the browser, with the list of all extensions it supports.
|
||||
|
||||
- `Sec-WebSocket-Protocol: soap, wamp` means that we'd like to transfer not just any data, but the data in [SOAP](http://en.wikipedia.org/wiki/SOAP) or WAMP ("The WebSocket Application Messaging Protocol") protocols. WebSocket subprotocols are registered in the [IANA catalogue](http://www.iana.org/assignments/websocket/websocket.xml). So, this header describes data formats that we're going to use.
|
||||
- `Sec-WebSocket-Protocol: soap, wamp` means that we'd like to transfer not just any data, but the data in [SOAP](https://en.wikipedia.org/wiki/SOAP) or WAMP ("The WebSocket Application Messaging Protocol") protocols. WebSocket subprotocols are registered in the [IANA catalogue](https://www.iana.org/assignments/websocket/websocket.xml). So, this header describes the data formats that we're going to use.
|
||||
|
||||
This optional header is set using the second parameter of `new WebSocket`. That's the array of subprotocols, e.g. if we'd like to use SOAP or WAMP:
|
||||
|
||||
|
@ -173,7 +173,7 @@ In the browser, we directly work only with text or binary frames.
|
|||
|
||||
**WebSocket `.send()` method can send either text or binary data.**
|
||||
|
||||
A call `socket.send(body)` allows `body` in string or a binary format, including `Blob`, `ArrayBuffer`, etc. No settings required: just send it out in any format.
|
||||
A call `socket.send(body)` allows `body` in string or a binary format, including `Blob`, `ArrayBuffer`, etc. No settings are required: just send it out in any format.
|
||||
|
||||
**When we receive the data, text always comes as string. And for binary data, we can choose between `Blob` and `ArrayBuffer` formats.**
|
||||
|
||||
|
@ -221,7 +221,7 @@ socket.close([code], [reason]);
|
|||
- `code` is a special WebSocket closing code (optional)
|
||||
- `reason` is a string that describes the reason of closing (optional)
|
||||
|
||||
Then the other party in `close` event handler gets the code and the reason, e.g.:
|
||||
Then the other party in the `close` event handler gets the code and the reason, e.g.:
|
||||
|
||||
```js
|
||||
// closing party:
|
||||
|
@ -249,7 +249,7 @@ There are other codes like:
|
|||
|
||||
The full list can be found in [RFC6455, §7.4.1](https://tools.ietf.org/html/rfc6455#section-7.4.1).
|
||||
|
||||
WebSocket codes are somewhat like HTTP codes, but different. In particular, any codes less than `1000` are reserved, there'll be an error if we try to set such a code.
|
||||
WebSocket codes are somewhat like HTTP codes, but different. In particular, codes lower than `1000` are reserved, there'll be an error if we try to set such a code.
|
||||
|
||||
```js
|
||||
// in case connection is broken
|
||||
|
@ -321,8 +321,8 @@ Server-side code is a little bit beyond our scope. Here we'll use Node.js, but y
|
|||
The server-side algorithm will be:
|
||||
|
||||
1. Create `clients = new Set()` -- a set of sockets.
|
||||
2. For each accepted websocket, add it to the set `clients.add(socket)` and setup `message` event listener to get its messages.
|
||||
3. When a message received: iterate over clients and send it to everyone.
|
||||
2. For each accepted websocket, add it to the set `clients.add(socket)` and set `message` event listener to get its messages.
|
||||
3. When a message is received: iterate over clients and send it to everyone.
|
||||
4. When a connection is closed: `clients.delete(socket)`.
|
||||
|
||||
```js
|
||||
|
@ -359,7 +359,7 @@ Here's the working example:
|
|||
|
||||
[iframe src="chat" height="100" zip]
|
||||
|
||||
You can also download it (upper-right button in the iframe) and run locally. Just don't forget to install [Node.js](https://nodejs.org/en/) and `npm install ws` before running.
|
||||
You can also download it (upper-right button in the iframe) and run it locally. Just don't forget to install [Node.js](https://nodejs.org/en/) and `npm install ws` before running.
|
||||
|
||||
## Summary
|
||||
|
||||
|
@ -383,6 +383,6 @@ Events:
|
|||
|
||||
WebSocket by itself does not include reconnection, authentication and many other high-level mechanisms. So there are client/server libraries for that, and it's also possible to implement these capabilities manually.
|
||||
|
||||
Sometimes, to integrate WebSocket into existing project, people run WebSocket server in parallel with the main HTTP-server, and they share a single database. Requests to WebSocket use `wss://ws.site.com`, a subdomain that leads to WebSocket server, while `https://site.com` goes to the main HTTP-server.
|
||||
Sometimes, to integrate WebSocket into existing projects, people run a WebSocket server in parallel with the main HTTP-server, and they share a single database. Requests to WebSocket use `wss://ws.site.com`, a subdomain that leads to the WebSocket server, while `https://site.com` goes to the main HTTP-server.
|
||||
|
||||
Surely, other ways of integration are also possible.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue