tornadoweb / tornadoweb/tornado

WebSocket with regex route doesn't call open callback

Open
#1,863 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

websocket
Dominant language
Python
Stars
22.2k
Forks
5.6k
Avg merge
3h 42m
Merged PRs (30d)
16

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided minimal example with both /ws and /nws/abc, then inspect tornado.web.Application route handling and the tornado.websocket.WebSocketHandler.open lifecycle. Done means the open callback is invoked for the regex route while the existing message and close behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.