Created
August 10, 2015 15:02
-
-
Save ax003d/42bde1f118dd888798f6 to your computer and use it in GitHub Desktop.
tornado websocket client
This file contains hidden or 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
import tornado | |
from tornado.websocket import websocket_connect | |
class WSClient(object): | |
def __init__(self, url): | |
self.url = url | |
self.ioloop = tornado.ioloop.IOLoop.current() | |
def start(self): | |
websocket_connect( | |
self.url, | |
self.ioloop, | |
callback=self.on_connected, | |
on_message_callback=self.on_message) | |
self.ioloop.start() | |
def on_connected(self, f): | |
try: | |
conn = f.result() | |
conn.write_message("hello") | |
except Exception, e: | |
print str(e) | |
self.ioloop.stop() | |
def on_message(self, msg): | |
print msg | |
if msg.endswith("hello"): | |
self.ioloop.stop() | |
if __name__=='__main__': | |
wsc = WSClient('ws://localhost:8000') | |
wsc.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment