I have the following basic tornado app:
import tornado.web
class IndexHandler(tornado.web.RequestHandler):
"""Regular HTTP handler to serve the ping page"""
def get(self):
self.write("OK")
if __name__ == "__main__":
app = tornado.web.Application([
(r"/", IndexHandler),
])
app.listen(8000)
print 'Listening on 0.0.0.0:8000'
tornado.ioloop.IOLoop.instance().start()
This will run on "http://localhost:8000"
. How would I get this to run and accept connections at ws://localhost:8000
?
tornado.web.RequestHandler
is used for accepting HTTP requests. For websockets, you need to use tornado.websocket.WebSocketHandler
.
Another thing to note is that you can't visit a websocket url directly from the browser. That is, you can't type ws://localhost:8000
in the address bar and expect to connect to the websocket. That is not how websockets work.
A websocket connection is an upgrage connection. Which means, you first have to visit a url via HTTP and then use Javascript to upgrade to websocket.
See an example about how to connect to websocket using Javascript at Mozilla Web Docs.