Skip to content

Instantly share code, notes, and snippets.

@ax003d
Created August 10, 2015 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ax003d/42bde1f118dd888798f6 to your computer and use it in GitHub Desktop.
Save ax003d/42bde1f118dd888798f6 to your computer and use it in GitHub Desktop.
tornado websocket client
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