【问题标题】:sockjs - example of implementing roomssockjs - 实现房间的例子
【发布时间】:2013-04-08 02:01:06
【问题描述】:

我想让用户创建并加入一个“房间”,以便他们可以协作。

我正在查看SockJs Multiplexer server 并想知道我是否可以利用其中的一些来创建和广播到特定的频道/房间。

在示例中,channel is created manuallyclient connects to that channel

将这些频道视为房间有用吗?

有没有办法动态创建这些通道,而不是在服务器上手动声明它们?

【问题讨论】:

    标签: python tornado sockjs


    【解决方案1】:

    免责声明:在我写完答案之前,我没有注意到您指的是服务器的 Javascript 版本。因此,我的代码示例是使用基于Python的sockjs-tornado给出的,但我猜原理应该是一样的。

    是的,您可以通过在每个SockJSConnection 派生类中保留一组引用连接来将通道视为房间。为此,您只需将每个连接类从

    class AnnConnection(SockJSConnection):
        def on_open(self, info):
            self.send('Ann says hi!!')
    
        def on_message(self, message):
            self.broadcast(self.connected, 'Ann nods: ' + message)
    

    class AnnConnection(SockJSConnection):
        connected = WeakSet()
    
        def on_open(self, info):
            self.connected.add(self)
            self.send('Ann says hi!!')
    
        def on_message(self, message):
            self.broadcast(self.connected, 'Ann nods: ' + message)
    
        def on_close(self):
            self.connected.remove(self)
            super(AnnConnection, self).on_close()
    

    server.py.

    为了动态创建频道,我稍微更改了__main__ 代码和MultiplexConnection 类并添加了几个新类。在if __name__ == "__main__": 块中,

    # Create multiplexer
    router = MultiplexConnection.get(ann=AnnConnection, bob=BobConnection,
                                     carl=CarlConnection)
    

    改为

    # Create multiplexer
    router = MultiplexConnection.get()
    

    虽然

    class MultiplexConnection(conn.SockJSConnection):
        channels = dict()
        # …
    
        def on_message(self, msg):
            # …
            if chan not in self.channels:
                return
    

    MultiplexConnection 中改为

    class MultiplexConnection(conn.SockJSConnection):
        channels = dict()
        _channel_factory = ChannelFactory()
        # …
    
        def on_message(self, msg):
            # …
            if chan not in self.channels:
                self.channels[chan] = self._channel_factory(chan)
    

    和类

    class DynamicConnection(SockJSConnection):
        def on_open(self, info):
            self.connected.add(self)
            self.send('{0} says hi!!'.format(self.name))
    
        def on_message(self, message):
            self.broadcast(self.connected, '{0} nods: {1}'
                .format(self.name, message))
    
        def on_close(self):
            self.connected.remove(self)
            super(DynamicConnection, self).on_close()
    
    
    class ChannelFactory(object):
        _channels = dict()
    
        def __call__(self, channel_name):
            if channel_name not in self._channels:
                class Channel(DynamicConnection):
                    connected = WeakSet()
                    name = channel_name
    
                self._channels[channel_name] = Channel
    
            return self._channels[channel_name]
    

    已添加到multiplex.py

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多