Which statement is true about the relationship between HTTP and WebSockets?
Click on the arrows to vote for the correct answer
A. B. C. D.D.
The correct answer is B: A WebSocket connection is initialized with an HTTP handshake.
Explanation:
WebSocket is a communication protocol that provides full-duplex communication channels over a single TCP connection. It allows a server to push data to a client as soon as it becomes available, without the need for the client to continuously poll the server for new data. WebSocket is designed to work over HTTP and to be compatible with HTTP proxies and intermediaries.
WebSocket connections are initiated with an HTTP handshake. When a client wants to establish a WebSocket connection to a server, it sends an HTTP request to the server with a special header, "Upgrade: websocket", indicating that it wants to upgrade the connection to a WebSocket connection. If the server agrees to the upgrade, it responds with an HTTP response with a status code of 101, indicating that the connection has been switched to a WebSocket connection.
Once the WebSocket connection is established, the client and server can exchange messages in either direction at any time, without the need for a new HTTP request or response. The WebSocket protocol defines its own message format and framing, which is different from the message format used in HTTP.
Therefore, option B is the correct answer as it correctly states that a WebSocket connection is initialized with an HTTP handshake. Option A is incorrect because while WebSocket does support bi-directional communication, it is not an HTTP session. Option C is incorrect because WebSocket connections can only be initiated by the client, but the server must agree to the upgrade request. Option D is incorrect because although WebSocket is designed to work over HTTP, it uses a different message format and framing than HTTP.