Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions amitu/socketio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def __init__(self, server, port, protocol="ws", *args, **kw):
self.protocol = protocol
self.handlers = {}

def run(self):
conn = httplib.HTTPConnection(self.server + ":" + str(self.port))
conn.request('POST','/socket.io/1/')
hskey = conn.getresponse().read().split(":")[0]
Expand All @@ -138,8 +137,9 @@ def run(self):
self.protocol, self.server, self.port, hskey
), *self.args, **self.kw
)
while True:
super(SocketIOClient, self).run()

def run(self):
super(SocketIOClient, self).run()

def on(self, name, callback):
self.handlers.setdefault(name, []).append(callback)
Expand Down
17 changes: 16 additions & 1 deletion amitu/websocket_client.py
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"
Expand All @@ -12,11 +13,13 @@ def __init__(self, value):
def __str__(self): return str(self.value)


class WebSocket(object):
class WebSocket(Thread):
Copy link
Owner

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.

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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The 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)).

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down