Allow sending empty messages

This is perfectly valid in the protocol, and may be meningful to some
applications. However send() is still stream oriented so it will ignore
an empty buffer.
This commit is contained in:
Pierre Ossman 2020-09-30 14:51:07 +02:00
parent 536a548db5
commit d72ace2ae6
1 changed files with 4 additions and 2 deletions

View File

@ -421,6 +421,9 @@ class WebSocket(object):
WebSocketWantWriteError can be raised if there is insufficient WebSocketWantWriteError can be raised if there is insufficient
space in the underlying socket. space in the underlying socket.
""" """
if len(bytes) == 0:
return 0
return self.sendmsg(bytes) return self.sendmsg(bytes)
def sendmsg(self, msg): def sendmsg(self, msg):
@ -435,8 +438,7 @@ class WebSocket(object):
""" """
if not self._sent_close: if not self._sent_close:
# Only called to flush? # Only called to flush?
if msg: self._sendmsg(0x2, msg)
self._sendmsg(0x2, msg)
self._flush() self._flush()
return len(msg) return len(msg)