-
-
Save majek/0418dc998fd7ec5b3d4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ws4py.client.threadedclient import WebSocketClient | |
import Queue | |
class WebSocket8Client(object): | |
class ConnectionClosedException(Exception): pass | |
def __init__(self, url): | |
queue = Queue.Queue() | |
self.queue = queue | |
class IntWebSocketClient(WebSocketClient): | |
def received_message(self, m): | |
queue.put(str(m)) | |
def read_from_connection(self, amount): | |
r = super(IntWebSocketClient, self).read_from_connection(amount) | |
if not r: | |
queue.put(Ellipsis) | |
return r | |
self.client = IntWebSocketClient(url) | |
self.client.connect() | |
def close(self): | |
if self.client: | |
self.client.running = False | |
self.client.close() | |
self.client._th.join() | |
self.client = None | |
def send(self, data): | |
self.client.send(data) | |
def recv(self): | |
try: | |
r = self.queue.get(timeout=1.0) | |
if r is Ellipsis: | |
raise self.ConnectionClosedException() | |
return r | |
except: | |
self.close() | |
raise | |
ws = WebSocket8Client('http://127.0.0.1:8000/') | |
print ws.send('a') | |
print ws.recv() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment