introduced close timeout
This commit is contained in:
liris
2015-07-30 10:30:12 +09:00
parent ec06e86588
commit d00154724b
2 changed files with 8 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ ChangeLog
- fix typo in README.rst (#197)
- introduce on_data callback to pass data type. (#198)
- WebSocketBadStatusException for Handshake error (#199)
- set close timeout (#192)
- 0.32.0

View File

@@ -422,13 +422,16 @@ class WebSocket(object):
self.connected = False
self.send(struct.pack('!H', status) + reason, ABNF.OPCODE_CLOSE)
def close(self, status=STATUS_NORMAL, reason=six.b("")):
def close(self, status=STATUS_NORMAL, reason=six.b(""), timeout=3):
"""
Close Websocket object
status: status code to send. see STATUS_XXX.
reason: the reason to close. This must be string.
timeout: timeout until recieve a close frame.
If None, it will wait forever until recieve a close frame.
"""
if self.connected:
if status < 0 or status >= ABNF.LENGTH_16:
@@ -437,8 +440,8 @@ class WebSocket(object):
try:
self.connected = False
self.send(struct.pack('!H', status) + reason, ABNF.OPCODE_CLOSE)
timeout = self.sock.gettimeout()
self.sock.settimeout(3)
sock_timeout = self.sock.gettimeout()
self.sock.settimeout(timeout)
try:
frame = self.recv_frame()
if isEnabledForError():
@@ -447,7 +450,7 @@ class WebSocket(object):
error("close status: " + repr(recv_status))
except:
pass
self.sock.settimeout(timeout)
self.sock.settimeout(sock_timeout)
self.sock.shutdown(socket.SHUT_RDWR)
except:
pass