Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket with regex route doesn't call open callback #1863

Open
M425 opened this issue Oct 22, 2016 · 1 comment
Open

WebSocket with regex route doesn't call open callback #1863

M425 opened this issue Oct 22, 2016 · 1 comment

Comments

@M425
Copy link

M425 commented Oct 22, 2016

I binded a tornado.websocket.WebSocketHandler with a normal route like "/ws" and it works as expected. However when i bind the handler with a regex route like "/nws/(.*)" it doesn't call the "open" callback.
Follows the shortest example i could build to show the issue:

import os
import tornado
import tornado.web
import tornado.gen
import tornado.websocket

class SocketHandler(tornado.websocket.WebSocketHandler):
    def initialize(self):
        print 'initialized'

    def check_origin(self, origin):
        return True

    @tornado.gen.coroutine
    def open(self):
        print 'opened'
        raise tornado.gen.Return()

    @tornado.gen.coroutine
    def on_message(self, message):
        print 'on_message {}'.format(message)
        self.write_message('resp: {}'.format(message))

    @tornado.gen.coroutine
    def on_close(self):
        print 'closed'


application = tornado.web.Application([
    (r"/ws",               SocketHandler),
    (r"/nws/(.*)",         SocketHandler),
])
application.listen(8181)
print 'Server started on port: {}, pid: {}'.format(8181, os.getpid())
tornado.ioloop.IOLoop.instance().start()

So I run a normal client which fires the following operation:

  • open
  • send message "test"
  • close

here the output when the path is /ws

initialized
opened
on_message test
closed

here the output when the path is /nws/abc

initialized
on_message test
closed
@M425
Copy link
Author

M425 commented Oct 22, 2016

Found the solution:
basically the function argument is also needed in the open callback, like this:

    @tornado.gen.coroutine
    def open(self, test):
        print 'opened'
        raise tornado.gen.Return()

however it seems like the _run_callback method of the WebSocketProtocol class in websocket.py doesn't correctly raise the exception when the arguments don't match

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants