Skip to content

Commit

Permalink
Call the WebSocketHandler.on_close method regardless of which side cl…
Browse files Browse the repository at this point in the history
…osed.

Closes #1173.
  • Loading branch information
bdarnell committed Jan 19, 2015
1 parent 7ce0df4 commit 05c3073
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tornado/test/websocket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def get(self):

class CloseReasonHandler(TestWebSocketHandler):
def open(self):
self.on_close_called = False
self.close(1001, "goodbye")


Expand Down Expand Up @@ -221,6 +222,8 @@ def test_server_close_reason(self):
self.assertIs(msg, None)
self.assertEqual(ws.close_code, 1001)
self.assertEqual(ws.close_reason, "goodbye")
# The on_close callback is called no matter which side closed.
yield self.close_future

@gen_test
def test_client_close_reason(self):
Expand Down
3 changes: 3 additions & 0 deletions tornado/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def __init__(self, application, request, **kwargs):
self.close_code = None
self.close_reason = None
self.stream = None
self._on_close_called = False

@tornado.web.asynchronous
def get(self, *args, **kwargs):
Expand Down Expand Up @@ -350,6 +351,8 @@ def on_connection_close(self):
if self.ws_connection:
self.ws_connection.on_connection_close()
self.ws_connection = None
if not self._on_close_called:
self._on_close_called
self.on_close()

def send_error(self, *args, **kwargs):
Expand Down

0 comments on commit 05c3073

Please sign in to comment.