Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket Close Packet

Tags:

c#

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?

like image 987
user2498443 Avatar asked Jun 18 '13 19:06

user2498443


People also ask

How do I close a WebSocket connection?

close() The WebSocket. close() method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED , this method does nothing.

What does WebSocket closed mean?

The WebSocket is closed before the connection is established error message indicates that some client code, or other mechanism, has closed the websocket connection before the connection was fully established.

Can WebSockets close servers?

Closing a connection is possible with the help of onclose event. After marking the end of communication with the help of onclose event, no messages can be further transferred between the server and the client. Closing the event can occur due to poor connectivity as well. The close() method stands for goodbye handshake.

What causes WebSocket to close?

This is likely due to client security rules in the WebSocket spec to prevent abusing WebSocket. (such as using it to scan for open ports on a destination server, or for generating lots of connections for a denial-of-service attack).


1 Answers

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:

  • (1 byte) flags and opcode
  • (variable number of bytes) mask flag and payload length
  • (optional, 4 bytes) mask
  • ({length} bytes) payload

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

like image 171
Marc Gravell Avatar answered Sep 19 '22 05:09

Marc Gravell