Hello world with Tornado and Javascript client - Stack Overflow

admin2025-04-21  0

I would like to establish a simple two-way munication between a Tornado server and a Javascript client.

When I run the server, followed by opening the page, the server prints out "get" as expected. However, while the server sends something back, the onmessage event never seems to be triggered in the client. Moreover, the Javascript console produces the following error:

WebSocket connection to 'ws://localhost:8888/' failed: Error during WebSocket handshake: Unexpected response code: 200

I have no clue what I am doing wrong here.

Python server (tornadoserver.py):

import tornado.ioloop
import tornado.web

class Hello(tornado.web.RequestHandler):
    def get(self):
        print("get")
        self.write("Hello, world")
        self.flush()


application = tornado.web.Application([
    (r"/", Hello),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

Javascript client (tornado.html):

<script>
ws = new WebSocket("ws://localhost:8888/");
ws.onmessage = function(e) {
    alert('message received: ' + e.data);
};
</script>

I would like to establish a simple two-way munication between a Tornado server and a Javascript client.

When I run the server, followed by opening the page, the server prints out "get" as expected. However, while the server sends something back, the onmessage event never seems to be triggered in the client. Moreover, the Javascript console produces the following error:

WebSocket connection to 'ws://localhost:8888/' failed: Error during WebSocket handshake: Unexpected response code: 200

I have no clue what I am doing wrong here.

Python server (tornadoserver.py):

import tornado.ioloop
import tornado.web

class Hello(tornado.web.RequestHandler):
    def get(self):
        print("get")
        self.write("Hello, world")
        self.flush()


application = tornado.web.Application([
    (r"/", Hello),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

Javascript client (tornado.html):

<script>
ws = new WebSocket("ws://localhost:8888/");
ws.onmessage = function(e) {
    alert('message received: ' + e.data);
};
</script>
Share Improve this question asked Mar 5, 2014 at 1:07 Bartm85Bartm85 431 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You need a separate WebSocketHandler instance mapped to a separate URL in your application. So your "tornado.html" should be mapped to the URL "/", and your Javascript should connect to a WebSocketHandler listening on a URL like "/websocket". Try this:

import tornado.ioloop
import tornado.web
import tornado.websocket


class Hello(tornado.websocket.WebSocketHandler):
    def open(self):
        self.write_message("Hello, world")

    def on_message(self, message):
        pass

    def on_close(self):
        pass


class Main(tornado.web.RequestHandler):
    def get(self):
        # This could be a template, too.
        self.write('''
<script>
ws = new WebSocket("ws://localhost:8888/websocket");
ws.onmessage = function(e) {
    alert('message received: ' + e.data);
};
</script>''')


application = tornado.web.Application([
    (r"/", Main),
    (r"/websocket", Hello),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745201141a290060.html

最新回复(0)