-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
websocketjs: Use Normal Closure (1000) close code in WebSocket.Close. #22
Conversation
Thanks for the pull request. Would you mind telling how I can reproduce the before/after behaviors and test this PR? |
@shurcooL I discovered this when I was using the websocket bindings with the gorilla websocket implementation server side. The library returns a CloseError with a Code which can be used to test the behaviour. The old behaviour defaulted to |
Sorry for the delay. I had to find a block of time to be able to review this. I can reproduce this with github.com/gorilla/websocket as the WebSocket server:
But not with The docs of https://developer.mozilla.org/en-US/docs/Web/API/WebSocket say:
However, the status codes are documented at https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent as:
So getting 1005 when a 1000 code is not explicitly provided makes sense. I'm not sure what they mean by "If this parameter is not specified, a default value of 1000 (indicating a normal "transaction complete" closure) is assumed." Perhaps they meant that "1000 can be assumed", although the real status code is 1005? But that might be a decision/bug/feature in gorilla/websocket... In any case, I think explicitly providing 1000 close reason in this library's Close method makes sense, and I'm in favor of doing that. I think it should also let us get rid of this case in the tests: Line 62 in 15f7e1f
|
Perhaps not. I misunderstood what it meant at first. The test uses |
I think it's more accurate to rephrase that as follows: When no status code is provided in the close frame, the other side may interpret that as 1005 instead of 1000. |
Looking at the code more, it does use 1000 for its |
When no status code is provided, the receiving side can interpret that to be status code 1005, indicating that the websocket was closed with no status code. In the case of the Close function, the user has requested a termination of the websocket and the status code should indicate that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've amended your commit to make minor adjustments based on findings above. I've created a const for the close code. Also renamed the consts in the tests to match the exact names from the spec (see https://tools.ietf.org/html/rfc6455#section-11.7).
I've ran the tests, and they passed.
LGTM.
Thanks @johanbrandhorst!
@nightexcessive Does this look good to you?
I appreciate that you're so thorough with this @shurcooL :). Thanks for the research! |
When no status code is provided, Close will default to 1005, indicating that the websocket was closed with no status code. In the case of the Close function, the user has requested a termination of the websocket and the status code should indicate that.