-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Threaded Version #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import ssl, socket, urlparse | ||
from mimetools import Message | ||
from StringIO import StringIO | ||
from threading import Thread | ||
|
||
FRAME_START = "\x00" | ||
FRAME_END = "\xff" | ||
|
@@ -12,11 +13,13 @@ def __init__(self, value): | |
def __str__(self): return str(self.value) | ||
|
||
|
||
class WebSocket(object): | ||
class WebSocket(Thread): | ||
def __init__( | ||
self, url, ca_certs=None, cert_reqs=ssl.CERT_NONE, headers=None, | ||
protocol=None, timeout=None | ||
): | ||
Thread.__init__(self) | ||
self.is_ready = False | ||
self.url = url | ||
self.ca_certs = ca_certs | ||
self.cert_reqs = cert_reqs | ||
|
@@ -97,12 +100,16 @@ def _consume_frames(self, buf): | |
return buf | ||
|
||
def run(self): | ||
self.is_stopped = False | ||
self._connect_and_send_handshake() | ||
buf = self._receive_handshake() | ||
|
||
self.onopen() | ||
|
||
self.is_ready = True | ||
while True: | ||
if self.stopped(): | ||
return | ||
buf = self._consume_frames(buf) | ||
|
||
try: | ||
|
@@ -116,6 +123,14 @@ def run(self): | |
def send(self, data): | ||
self.sock.send('\x00' + unicode(data).encode("utf-8") + '\xff') | ||
|
||
def ready(self): | ||
return self.is_ready | ||
def stop(self): | ||
self.is_stopped = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. threading.Event is thread safe way to do this: http://docs.python.org/release/2.3.5/lib/event-objects.html (variable changes in one thread is not guaranteed to be "visible" (http://stackoverflow.com/questions/3549833/python-threading-memory-model-and-visibility)). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah I knew there was a smarter way to do this, I'll peek at that tomorrow and refactor these methods |
||
|
||
def stopped(self): | ||
return self.is_stopped | ||
|
||
def onopen(self): pass | ||
def onmessage(self, message): pass | ||
def onclose(self): pass | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a ThreadedWebSocket class that derives from WebSocket? I am using this library in Qt and am planning to use Qt's thread with their signals and slots, so am writing QThreadedWebSocket.