Make websocket's API more intuitive
Functions connect() and accept() are using http functionality, like sending requests and headers. Let's create separate functions with more intuitive names for these calls. This allows subclasses to override these functions, as well as makes the code easier to understand at a glance.
This commit is contained in:
parent
c123bfbbff
commit
832ae23f00
|
|
@ -158,19 +158,19 @@ class WebSocket(object):
|
||||||
if not path:
|
if not path:
|
||||||
path = "/"
|
path = "/"
|
||||||
|
|
||||||
self._queue_str("GET %s HTTP/1.1\r\n" % path)
|
self.send_request("GET", path)
|
||||||
self._queue_str("Host: %s\r\n" % uri.hostname)
|
self.send_header("Host", uri.hostname)
|
||||||
self._queue_str("Upgrade: websocket\r\n")
|
self.send_header("Upgrade", "websocket")
|
||||||
self._queue_str("Connection: upgrade\r\n")
|
self.send_header("Connection", "upgrade")
|
||||||
self._queue_str("Sec-WebSocket-Key: %s\r\n" % self._key)
|
self.send_header("Sec-WebSocket-Key", self._key)
|
||||||
self._queue_str("Sec-WebSocket-Version: 13\r\n")
|
self.send_header("Sec-WebSocket-Version", 13)
|
||||||
|
|
||||||
if origin is not None:
|
if origin is not None:
|
||||||
self._queue_str("Origin: %s\r\n" % origin)
|
self.send_header("Origin", origin)
|
||||||
if len(protocols) > 0:
|
if len(protocols) > 0:
|
||||||
self._queue_str("Sec-WebSocket-Protocol: %s\r\n" % ", ".join(protocols))
|
self.send_header("Sec-WebSocket-Protocol", ", ".join(protocols))
|
||||||
|
|
||||||
self._queue_str("\r\n")
|
self.end_headers()
|
||||||
|
|
||||||
self._state = "send_headers"
|
self._state = "send_headers"
|
||||||
|
|
||||||
|
|
@ -283,15 +283,15 @@ class WebSocket(object):
|
||||||
if self.protocol not in protocols:
|
if self.protocol not in protocols:
|
||||||
raise Exception('Invalid protocol selected')
|
raise Exception('Invalid protocol selected')
|
||||||
|
|
||||||
self._queue_str("HTTP/1.1 101 Switching Protocols\r\n")
|
self.send_response(101, "Switching Protocols")
|
||||||
self._queue_str("Upgrade: websocket\r\n")
|
self.send_header("Upgrade", "websocket")
|
||||||
self._queue_str("Connection: Upgrade\r\n")
|
self.send_header("Connection", "Upgrade")
|
||||||
self._queue_str("Sec-WebSocket-Accept: %s\r\n" % accept)
|
self.send_header("Sec-WebSocket-Accept", accept)
|
||||||
|
|
||||||
if self.protocol:
|
if self.protocol:
|
||||||
self._queue_str("Sec-WebSocket-Protocol: %s\r\n" % self.protocol)
|
self.send_header("Sec-WebSocket-Protocol", self.protocol)
|
||||||
|
|
||||||
self._queue_str("\r\n")
|
self.end_headers()
|
||||||
|
|
||||||
self._state = "flush"
|
self._state = "flush"
|
||||||
|
|
||||||
|
|
@ -447,6 +447,18 @@ class WebSocket(object):
|
||||||
|
|
||||||
return len(msg)
|
return len(msg)
|
||||||
|
|
||||||
|
def send_response(self, code, message):
|
||||||
|
self._queue_str("HTTP/1.1 %d %s\r\n" % (code, message))
|
||||||
|
|
||||||
|
def send_header(self, keyword, value):
|
||||||
|
self._queue_str("%s: %s\r\n" % (keyword, value))
|
||||||
|
|
||||||
|
def end_headers(self):
|
||||||
|
self._queue_str("\r\n")
|
||||||
|
|
||||||
|
def send_request(self, type, path):
|
||||||
|
self._queue_str("%s %s HTTP/1.1\r\n" % (type.upper(), path))
|
||||||
|
|
||||||
def ping(self, data=b''):
|
def ping(self, data=b''):
|
||||||
"""Write a ping message to the WebSocket
|
"""Write a ping message to the WebSocket
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue