Created
August 18, 2012 17:14
-
-
Save cjgiridhar/3388487 to your computer and use it in GitHub Desktop.
Tornado - WebSockets
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.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) |
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
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