Skip to content

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

Open
@M425

Description

@M425

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions