Skip to content
WebsiteHosting.com

concept HTTP, TLS & web servers

WebSocket handshake, frame flow and close states

An HTTP/1.1 upgrade lets both endpoints send WebSocket frames without a new request per message. A Close frame starts or answers the closing handshake; it does not prove the TCP connection has closed.

Latest recorded source check · P0199

What it means

In HTTP/1.1, WebSocket starts with an HTTP request to switch protocols. Once the opening handshake succeeds, either the client or the server can send WebSocket frames on the same connection. A message does not need a new HTTP request, and a frame traveling in the other direction is not necessarily a reply.

Data frames carry application data. Control frames have protocol duties: a Ping can call for a Pong, and a Close starts or answers the closing handshake. Exchanging Close frames and closing the underlying TCP connection are distinct events.

Try it

Simulation: examine one event from one endpoint’s point of view. The default choices are provided scenario evidence, a valid-101 opening handshake, an open endpoint and client-masked-data as the next event. The endpoint stays open. Its reply is none: this data frame does not require a protocol reply. The application can still send a message back.

WebSocket handshake, frame flow and close states

Show upgrade, masking, ping/pong and close handshake versus normal HTTP request flow

Simulation
Modeled outcome: Open

The scoped event is consistent with the open state.

WebSocket handshake, frame flow and close states: synthetic scenario inputs
Replaces all inputs with the documented starting example and the indicated change. Reset restores the starting example.
Handshake, frame event and close state

Handshake and event

  1. Upgradevalid-101
  2. Prior stateopen
  3. Frame or transport eventclient-masked-data

Result

  1. Protocol stateopen
  2. Transport evidenceNot evaluated / established
  3. Replynone
  4. Outcomeopen

The state after Close exchange and actual transport closure are different observations. The fixture models one HTTP/1.1 WebSocket session, not a live socket.

Evaluated inputs

Required scenario evidence
provided
Opening handshake evidence
valid-101
Endpoint state
open
Next symbolic event
client-masked-data

HTTP/1.1 upgrade only, symbolic one-endpoint close state; no raw frames, fragmentation, extensions, wire payload, actual socket or exploit. Closing cases conservatively block other modeled events; this is not full protocol state machine (in-flight data, Pong rules other than the included received Ping cases, and repeated Close not exhaustively represented).

About this model

The controls set only a local, bounded scenario. The computed branch is not a verdict about a live system. Inputs listed as not evaluated did not participate in this branch after validation.

Reset restores the original inputs. It does not change the open or closed state of help or FAQ answers.

  1. Change only Next symbolic event to ping, meaning the endpoint receives a Ping. The state is still open, but the reply changes to pong. It is the kind of frame, not a new HTTP request, that calls for a reply.
  2. Keep ping and change Endpoint state to close-sent. The endpoint is closing, but it still owes a Pong: it has sent its own Close and has not received the peer’s Close.
  3. Change Endpoint state to close-received instead. Now the reply is not required. Both examples describe a closing endpoint; the difference is which direction the Close traveled.

Each selection describes the state before the chosen event, not a running history. Reset returns all four choices to the masked-data example. No connection is opened and no frames are sent.

From HTTP to frames

The client waits for the server’s opening response before sending WebSocket data. Here valid-101 means the client has a valid HTTP/1.1 switching-protocols response, not merely that it saw the number 101. If the server rejects the handshake, the result stays at HTTP; the model does not evaluate the selected endpoint state or frame. If the response is unknown, there is no basis for saying the connection opened or that the server rejected it. This opening sequence is specific to HTTP/1.1. WebSocket over HTTP/2 uses extended CONNECT on a stream instead.

After a valid opening, direction matters. The client must mask the frames it sends to the server; the server must not mask frames it sends to the client. In the default example, changing masked client data to unmasked client data therefore changes the result from open to a protocol error. The server must close the connection. That requirement is not evidence that TCP closure has already happened.

Reading a Close from one side

Consider a client that has just sent Close. It must not send another data frame, even a correctly masked one. But its own Close says nothing yet about whether the server’s Close has arrived. Read the next observation before deciding what ended:

Observation at the clientWhat it establishes
Client sent Close; no peer Close observedThe client has started closing. Its new data sends must stop.
Client then received the server’s CloseThe Close exchange is complete. The transport must close; this event does not show that it did.
TCP closure observedThe transport has closed. This observation alone does not show that the peers exchanged Close frames first.

The simulation groups the last two outcomes under closed, so read the state and transport together. close-exchanged with close required describes the completed handshake; closed with transport closed describes a transport-closure event. While the handshake is in progress, transport not established means that closure has not been established by the evidence, not that the connection never opened.

Start in close-received and select send-close. The fixture reports close-exchanged and closure required, not an observed closed transport. Now select the transport-closed event instead: the state records actual closure under that premise. In a diagnostic record distinguish the last verified frame exchange from a later transport event; neither can be reconstructed from a generic closed label alone.

In practice

When reading a connection log, start with the opening response. Then identify the sender and the kind of frame. For a shutdown, look for three separate observations from the same endpoint: Close sent, Close received and transport closed. The simulation does not reconstruct in-flight data, fragmentation, extensions or every control-frame sequence, so do not use a blocked symbolic event to classify everything a peer might still receive.

If all you have is an application’s request to disconnect, the next useful observation is whether its endpoint actually sent Close. If both Close frames are accounted for, look for transport closure instead. Record the last event you can establish, not the one you expected to follow.

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

Does masking encrypt WebSocket data?

No; use TLS for transport confidentiality.

Are ping/pong application messages?

Ping and Pong are WebSocket control frames, not application data messages. Their processing is governed by the protocol and the connection’s closing state.

Can data continue after sending Close?

No further data frames may be sent by that endpoint after it sends Close. Close exchange and transport closure are different events; after the exchange, the model reports closure required rather than an observed closed transport.

Is this a live WebSocket client?

No. It evaluates symbolic events locally and creates no connection or frames.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. RFC 6455: The WebSocket Protocol

    4,5,7. Client frames require masking, including over TLS; receiving unmasked client frames requires server closure. Close sent prohibits further data sends; exchange precedes closing TCP. Received Ping requires Pong unless Close already received.

    Checked .

  2. RFC 8441: Bootstrapping WebSockets with HTTP/2

    §§4–5. WebSockets over HTTP/2 use extended CONNECT on a stream, rather than the HTTP/1.1 upgrade handshake modeled here.

    Checked .

  3. RFC 9931: Security Considerations for Optimistic Protocol Transitions in HTTP/1.1

    §6.2 and §8. CONNECT rejection requires connection closure under the server mitigation, with the specified exception for known compliant clients. A proxy client acting for an untrusted TCP client waits for 2xx or uses Connection: close, or both. WebSocket clients already wait for the handshake response.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.