Reputation: 153
I am trying to send a close packet to my client from my server artificially, what raw value should I send so the client will understand that I am closing the connection? Does it differ between websocket protocols?
Upvotes: 7
Views: 2448
Reputation: 1062820
At the simplest level - just close the connection - that'll work fine.
The very early hixie-76 has a simple sequence for signalling closure: just send the two bytes 0xFF, 0x00 (the first is the frame type, the second is the length).
In all later specifications, frames are a bit more complex; they are broken down as:
For the first part, the opcode we want is 0x08, and this must be combined with the "fin" flag, 0x80 (this just indicates that the frame isn't split over several messages) - so 0x88.
For the second part, we can use a zero-length message, but we need to know whether we are client-to-server (always masked, so we need to use 0x80) or server-to-client (never masked, so we need to use 0x00).
The third part is only included if the mask flag is set, so client-to-server; since we aren't sending a payload there is no point inventing a cryptographically secure mask - we can just use 0x00 0x00 0x00 0x00.
The fourth is omitted as we are stating zero length.
So: client-to-server: 0x88 0x80 0x00 0x00 0x00 0x00
And server-to-client: 0x88 0x00
Upvotes: 10