Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created August 18, 2012 17:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cjgiridhar/3388487 to your computer and use it in GitHub Desktop.
Save cjgiridhar/3388487 to your computer and use it in GitHub Desktop.
Tornado - WebSockets
import tornado.ioloop
import tornado.web
import tornado.websocket
class Socket(tornado.websocket.WebSocketHandler):
def open(self):
print "Socket opened"
def on_message(self, message):
self.write_message("Msg is " + message)
def on_close(self):
print "Socket closed"
class Main(tornado.web.RequestHandler):
def get(self):
print "It Works!"
application = tornado.web.Application([
(r"/", Main),
(r"/socket", Socket),
],debug=True)
from ws4py.client.threadedclient import WebSocketClient
class EchoClient(WebSocketClient):
def opened(self):
self.send("Hello Chetan")
def closed(self, code, reason):
print "Closed down", code, reason
def received_message(self, m):
print "Received from server: %s" % (str(m))
if __name__ == '__main__':
try:
ws = EchoClient('http://localhost:8888/socket', protocols=['http-only', 'chat'])
ws.daemon = False
ws.connect()
except KeyboardInterrupt:
ws.close()
finally:
ws.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment