WebSocketHandler does not call initialization

What I'm trying to do is have an object (this is a gstreamer process that runs in a separate thread) to be able to call the write_message () function on WebSocketHandler

Here is the code snippet that I currently have

app = tornado.web.Application([
    (r'/', RadioSocketHandler),
])


class RadioSocketHandler(tornado.websocket.WebSocketHandler):
    def initialize(self):
        self.player = MusicPlayer(self)
        thread.start_new(self.player.start(), ())

class MusicPlayer(object):
    websocket_handler = None

    def __init__(self, sockethandler):
        self.websocket_handler = sockethandler
        self.websocket_handler.write_message("success")

However, this will not work. "initialize" is never called. What am I doing wrong?

__init__()

doesn't work either. Or is there another way to call a function from RadioSocketHandler outside of its own class? I'm new to Python fyi

+4
source share
1 answer

Well, I got this while working with the following:

app = tornado.web.Application([
    (r'/', RadioSocketHandler, {'player': player}),
])

class RadioSocketHandler(tornado.websocket.WebSocketHandler):
    def __init__(self, *args, **kwargs):
        self.musicplayer = kwargs.pop('player')
        self.musicplayer.set_sockethandler(self)
        super(RadioSocketHandler, self).__init__(*args, **kwargs)

class MusicPlayer(object):
    websocket_handler = None

    def set_sockethandler(self, handler):
        self.websocket_handler = handler

I had to pass arguments to the init function differently. And I forgot super ()

+4

All Articles