Skip to content

Instantly share code, notes, and snippets.

@aburan28
Forked from cjgiridhar/tornadowebsocket.py
Created June 15, 2013 17:24
Show Gist options
  • Save aburan28/5788829 to your computer and use it in GitHub Desktop.
Save aburan28/5788829 to your computer and use it in GitHub Desktop.
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