Subject: [hybi] WebSocket closing handshake

From: Fumitoshi Ukai (鵜飼文敏) <[email-masked]>
Date: Tue, 18 May 2010 19:45:49 +0900

Hi,
I've question about WebSocket closing handshake.
closing handshake is described as follows:

   Once a WebSocket connection is established, the user agent must use
   the following steps to *start the WebSocket closing handshake*.
   These steps must be run asynchronously relative to whatever algorithm
   invoked this one.

   1.  If the WebSocket closing handshake has started, then abort these
       steps.

   2.  Send a 0xFF byte to the server.

   3.  Send a 0x00 byte to the server.

   4.  *The WebSocket closing handshake has started*.

   5.  Wait a user-agent-determined length of time, or until the
       WebSocket connection is closed.

   6.  If the WebSocket connection is not already closed, then close the
       WebSocket connection.  (If this happens, then the closing
       handshake doesn't finish.)

What means "Send a 0xXX byte to the server" in this context?
 A) Put 0xXX byte in WebSocket message queue to the server.  "Send 0xXX byte
to the server" finishes immediately after put in queue.
 B) Put 0xXX byte in WebSocket message queue to the server, and make sure
the byte was actually sent to the TCP connection.
 C) Send 0xXX byte to the server, ignoring WebSocket message queue.

I think option C is not reasonable.  The server would fail to parse
WebSocket frame, if 0xFF 0x00 might be sent in the middle of large WebSocket
frame.

If this means A, then can we "close the WebSocket connection" (close the TCP
connection) without sending bytes in WebSocket message queue to the server?
It would happen
   0xFF 0x00 frame is received
    If the WebSocket closing handshake has not yet started, then start the
WebSocket closing handshake.
           put 0xFF 0x00 to the queue.
           WebSocket closing handshake has started
    Wait until either the WebSocket closing handshake has started or the
WebSocket connection is closed.  Yes, WebSocket closing handshake has
started.
    If the WebSocket connection is not already closed, then close the
WebSocket connection.
            0xFF 0x00 may be still in queue, and try to close the TCP
connection.

If this means B, will we send 0xFF 0x00 several time?
For example,
   ws.close() is called in OPEN state, start the WebSocket closing
handshake.
    The WebSocket closing handshake has not yet started
    Put 0xFF 0x00 to the queue and wait for these byte sent to the TCP
connection.
    while waitng, receive 0xFF 0x00 frame, run the following substeps
      - If the WebSocket closing has not yet started, yes, it has not yet
finished sending 0xFF 0x00 bytes, so start the WebSocket closing handshake
again (put 0xFF 0x00 to the queue)

Or any other interpretation?

Thanks,
Fumitoshi Ukai