Skip to content

Commit 2f1821b

Browse files
Update article.md
1 parent 6976161 commit 2f1821b

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

5-network/11-websocket/article.md

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# WebSocket
22

3-
Il protocollo `WebSocket`, descritto nelle specifiche [RFC 6455](http://tools.ietf.org/html/rfc6455) fornisce una mezzo di scambio dati tra server e browser utilizzando una connessione persistente. I dati vengono passati in entrambe le direzioni come "pacchetti" (packets), senza la necessità di interrompere la connessione e senza la necessità di creare nuovi headers-HTTP per le richieste.
3+
Il protocollo `WebSocket`, descritto nelle specifiche [RFC 6455](http://tools.ietf.org/html/rfc6455) fornisce un mezzo di scambio dati tra server e browser utilizzando una connessione persistente. I dati vengono passati in entrambe le direzioni come "pacchetti" (packets), senza la necessità di interrompere la connessione e senza la necessità di creare nuovi headers-HTTP per le richieste.
44

55
I WebSocket sono particolarmente adatti per servizi che richiedono scambi continui di dati, come ad esempio giochi online, sistemi di trading in real-time e così via.
66

@@ -64,19 +64,22 @@ A scopo dimostrativo, c'è un piccolo server funzionante [server.js](demo/server
6464

6565
Quindi vedrai gli eventi `open` -> `message` -> `close`.
6666

67-
That's actually it, we can talk WebSocket already. Quite simple, isn't it?
67+
Questo è tutto, possiamo già parlare di WebSocket. Abbastanza semplice, no?
6868

69-
Now let's talk more in-depth.
69+
Adesso approfondiamo un po'.
7070

71-
## Opening a websocket
7271

73-
When `new WebSocket(url)` is created, it starts connecting immediately.
72+
## Aprire un WebSocket
7473

75-
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.
74+
75+
Non appena viene istanziato `new WebSocket(url)`, questo tenta immediatamente una connessione.
76+
77+
Dureante la connessione, il browser (utilizzando degli headers appositi) chiede al server: "Supporti i WebSocket?" e se il server risponde di "si", allora la conversazione continua con il protocollo WebSocket, che non è per nulla HTTP.
7678

7779
![](websocket-handshake.svg)
7880

79-
Here's an example of browser headers for request made by `new WebSocket("wss://javascript.info/chat")`.
81+
Questo è un esempio di headers impostati dal browser per la richiesta di `new WebSocket("wss://javascript.info/chat")`.
82+
8083

8184
```
8285
GET /chat
@@ -88,16 +91,16 @@ Sec-WebSocket-Key: Iv8io/9s+lYFgZWcXczP8Q==
8891
Sec-WebSocket-Version: 13
8992
```
9093

91-
- `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 compabitility issues. But `Origin` header is important, as it allows the server to decide whether or not to talk WebSocket with this website.
92-
- `Connection: Upgrade` -- signals that the client would like to change the protocol.
93-
- `Upgrade: websocket` -- the requested protocol is "websocket".
94-
- `Sec-WebSocket-Key` -- a random browser-generated key for security.
95-
- `Sec-WebSocket-Version` -- WebSocket protocol version, 13 is the current one.
94+
- `Origin` -- l'origine della pagina del client, ad esempio: `https://javascript.info`. Gli oggetti WebSocket sono per loro natura cross-origin. Non vi sono headers particolari o altre limitazioni. Server di vecchia data non gestiscono i WebSocket in nessun modo, quindi non ci sono problemi di compatibilità. Ma l'header `Origin` è importante, dal momento che permette al server di decidere se parlare o meno con quel sito.
95+
- `Connection: Upgrade` -- indica che il client vuole cambiare protocollo di comunicazione.
96+
- `Upgrade: websocket` -- il protocollo richiesto è "websocket".
97+
- `Sec-WebSocket-Key` -- una chiave generata randomicamente dal browser per sicurezza.
98+
- `Sec-WebSocket-Version` -- versione di protocollo del WebSocket, 13 è quella corrente.
9699

97-
```smart header="WebSocket handshake can't be emulated"
98-
We can't use `XMLHttpRequest` or `fetch` to make this kind of HTTP-request, because JavaScript is not allowed to set these headers.
100+
```smart header="L'handsnake del WebSocket non può essere emulato."
101+
Non è possibile utilizzare `XMLHttpRequest` oppure `fetch` per fare questo tipo di richieste HTTP, dal momento che a JavaScript non è peremessa la creazione di questi headers.
99102
```
100-
103+
Se il server acconsente allo switch in WebSocket, dovrebbe rispondere con un codice 101:
101104
If the server agrees to switch to WebSocket, it should send code 101 response:
102105

103106
```
@@ -107,29 +110,30 @@ Connection: Upgrade
107110
Sec-WebSocket-Accept: hsBlbuDTkk24srzEOTBUlZAlC2g=
108111
```
109112

110-
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.
113+
Qui `Sec-WebSocket-Accept` è `Sec-WebSocket-Key` ricodificato usando un algoritmo speciale. Il browser lo usa per assicurarsi che la risposta sia corrispondente alla richiesta.
114+
115+
In seguito, i dati vengono trasferiti usando il protocollo WebSocket, presto ne vedremo vedremo la struttura ("frames"). E questo non ha niente a che vedere con HTTP.
111116

112-
Afterwards, the data is transfered using WebSocket protocol, we'll see its structure ("frames") soon. And that's not HTTP at all.
117+
### Estensioni e subprotocolli
113118

114-
### Extensions and subprotocols
119+
Ci possono essere gli headers aggiuntivi `Sec-WebSocket-Extensions` e `Sec-WebSocket-Protocol` i quali descrivo etensioni e subprotocolli.
115120

116-
There may be additional headers `Sec-WebSocket-Extensions` and `Sec-WebSocket-Protocol` that describe extensions and subprotocols.
121+
Ad esempio:
117122

118-
For instance:
123+
- `Sec-WebSocket-Extensions: deflate-frame` significa che il browser supporta la compressione dei dati. Una estensione è un qualcosa di collegato al trasferimento dei dati, delle una funzionalità che estende il protocollo WebSocket. L'header `Sec-WebSocket-Extensions`, invece, viene inviato in automatico dal browser, con la lista di tutte le estensioni che può supportare.
119124

120-
- `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 extenions it supports.
125+
- `Sec-WebSocket-Protocol: soap, wamp` significa che non vogliamo trasferire qualunque tipo di dato, ma solamente dati con protocollo [SOAP](http://en.wikipedia.org/wiki/SOAP) oppure WAMP ("The WebSocket Application Messaging Protocol"). I subprotocolli dei WebSocket sono registrati nel [catalogo IANA ](http://www.iana.org/assignments/websocket/websocket.xml).
121126

122-
- `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).
123127

124-
This optional header is set by us, to tell the server which subprotocols our code supports, using the second (optional) parameter of `new WebSocket`. That's the array of subprotocols, e.g. if we'd like to use SOAP or WAMP:
128+
Questi headers aggiuntivi vengono settati da noi, per dire al server quali sottoprotocolli supporta il nostro codice, utilizzando il secondo parametro (opzionale) di `new WebSocket`. Questo sarebbe l'array dei subprotocolli se, ad esempio, volessimo usare SOAP o WAMP:
125129

126130
```js
127131
let socket = new WebSocket("wss://javascript.info/chat", ["soap", "wamp"]);
128132
```
129133

130-
The server should respond with a list of protocols and extensions that it agrees to use.
134+
Il server potrebbe rispondere con una lista di protocolli ed estensioni che acconsente di usare.
131135

132-
For example, the request:
136+
Per esempio, la richiesta:
133137

134138
```
135139
GET /chat
@@ -145,7 +149,7 @@ Sec-WebSocket-Protocol: soap, wamp
145149
*/!*
146150
```
147151
148-
Response:
152+
Risposta:
149153
150154
```
151155
101 Switching Protocols
@@ -157,10 +161,9 @@ Sec-WebSocket-Extensions: deflate-frame
157161
Sec-WebSocket-Protocol: soap
158162
*/!*
159163
```
164+
Qui il server risponde che supporta l'estensione "deflate-frame", e solamente SOAP tra i subprotocolli richiesti.
160165
161-
Here the server responds that it supports the extension "deflate-frame", and only SOAP of the requested subprotocols.
162-
163-
## Data transfer
166+
## Trasferimento dati
164167
165168
WebSocket communication consists of "frames" -- data fragments, that can be sent from either side, and can be of several kinds:
166169

0 commit comments

Comments
 (0)