Reputation: 21
The Trouble:
When i get and upgrade header that says "Upgrade: https" i have no idea what browser does next and that confuses me.
1) Does browser expect answer to which protoc it is upgraded ? 2) what it does next ? in case of https does it create new connection with https and keeps that cached somewhere ? 3) in case of web sockets ? does it close first handshake connection and establishes new connection ? and again does it cache info about what protoc to use ? 4) i know there is plenty of WS libs but iw ould like to know how it functions if i only use native nodejs modules.
big thanks in advance.
Upvotes: 1
Views: 3228
Reputation: 707806
The whole back and forth protocol scheme is documented very well here.
1) Does browser expect answer to which protoc it is upgraded ?
Yes, client sends this:
GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Server responds with this:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
2) what it does next ?
When it gets this confirmation from the server and the 101 response code, then the client knows that the server has switched the current socket to the webSocket protocol. This currently open socket (from the original http request) becomes the webSocket socket. Either the client or server are now free to send a webSocket formatted data frame to the other end.
3) in case of web sockets ? does it close first handshake connection and establishes new connection ?
No, the original socket from the initial http request becomes the webSocket connection.
and again does it cache info about what protoc to use
It's just part of the state each ends keeps about the original socket. No new socket is opened.
4) i know there is plenty of WS libs but iw ould like to know how it functions if i only use native nodejs modules.
It's good to understand how it works, but unless this is just an academic interest or project it's probably an inefficient use of development time to write your own webSocket protocol as it has been done by others many times and it's a fair amount of detail work to get everything correct and interoperable with some of the variations in the spec.
On top of what has already been described here, there's the data frame format, encryption keys, packet fragmentation, pings and pongs, sub protocol support, etc...
Upvotes: 5