improved websocket handling

This commit is contained in:
Jakob Ketterl 2019-10-11 12:08:43 +02:00
parent 2e75bac90c
commit f9f50e734f
1 changed files with 13 additions and 7 deletions

View File

@ -16,11 +16,19 @@ OPCODE_PING = 0x09
OPCODE_PONG = 0x0A OPCODE_PONG = 0x0A
class IncompleteRead(Exception): class WebSocketException(Exception):
pass pass
class Drained(Exception): class IncompleteRead(WebSocketException):
pass
class Drained(WebSocketException):
pass
class WebSocketClosed(WebSocketException):
pass pass
@ -93,6 +101,8 @@ class WebSocketConnection(object):
return bytes([ws_first_byte, size]) return bytes([ws_first_byte, size])
def send(self, data): def send(self, data):
if not self.open:
raise WebSocketClosed()
# convenience # convenience
if type(data) == dict: if type(data) == dict:
# allow_nan = False disallows NaN and Infinty to be encoded. Browser JSON will not parse them anyway. # allow_nan = False disallows NaN and Infinty to be encoded. Browser JSON will not parse them anyway.
@ -151,7 +161,7 @@ class WebSocketConnection(object):
WebSocketConnection.connections.append(self) WebSocketConnection.connections.append(self)
self.open = True self.open = True
while self.open: while self.open:
(read, _, _) = select.select([self.interruptPipeRecv, self.handler.rfile], [], []) (read, _, _) = select.select([self.interruptPipeRecv, self.handler.rfile], [], [], 15)
if self.handler.rfile in read: if self.handler.rfile in read:
available = True available = True
self.resetPing() self.resetPing()
@ -232,7 +242,3 @@ class WebSocketConnection(object):
def sendPong(self): def sendPong(self):
header = self.get_header(0, OPCODE_PONG) header = self.get_header(0, OPCODE_PONG)
self._sendBytes(header) self._sendBytes(header)
class WebSocketException(Exception):
pass