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
+32-29Lines changed: 32 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# WebSocket
2
2
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.
4
4
5
5
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.
6
6
@@ -64,19 +64,22 @@ A scopo dimostrativo, c'è un piccolo server funzionante [server.js](demo/server
64
64
65
65
Quindi vedrai gli eventi `open` -> `message` -> `close`.
66
66
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?
68
68
69
-
Now let's talk more in-depth.
69
+
Adesso approfondiamo un po'.
70
70
71
-
## Opening a websocket
72
71
73
-
When `new WebSocket(url)` is created, it starts connecting immediately.
72
+
## Aprire un WebSocket
74
73
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.
76
78
77
79

78
80
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")`.
-`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.
96
99
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.
99
102
```
100
-
103
+
Se il server acconsente allo switch in WebSocket, dovrebbe rispondere con un codice 101:
101
104
If the server agrees to switch to WebSocket, it should send code 101 response:
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.
111
116
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
113
118
114
-
### Extensions and subprotocols
119
+
Ci possono essere gli headers aggiuntivi `Sec-WebSocket-Extensions` e `Sec-WebSocket-Protocol` i quali descrivo etensioni e subprotocolli.
115
120
116
-
There may be additional headers `Sec-WebSocket-Extensions` and `Sec-WebSocket-Protocol` that describe extensions and subprotocols.
121
+
Ad esempio:
117
122
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.
119
124
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).
121
126
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).
123
127
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:
125
129
126
130
```js
127
131
let socket =newWebSocket("wss://javascript.info/chat", ["soap", "wamp"]);
128
132
```
129
133
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.
0 commit comments