本文介绍了sockjs - 实现房间的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户创建并加入一个房间",以便他们进行协作.

I would like to have users create and join a "rooms" so that they can collaborate.

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

I'm looking at SockJs Multiplexer server and wondering if I can leverage some of that to create and broadcast to specific channel/room.

在示例中,创建了一个 频道手动客户端连接到该频道.

In the example, a channel is created manually and client connects to that channel.

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

Would treating these channels as rooms work?

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

Is there a way to dynamically create these channels instead of manually declaring them on server?

推荐答案

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

Disclaimer: I didn't notice that you were referring to the Javascript-version of the server until I had written my answer. Therefore, my code examples are given using the Python-based sockjs-tornado, but I guess the principles should be the same.

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

Yes, you may treat the channels as rooms by keeping a set of referenced connections in each SockJSConnection-derived class. To do that, all you would need is to change each connection class slightly from

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__": 块中,

To create the channels dynamically, I changed the __main__ code and MultiplexConnection class slightly and added a couple of new classes. In the if __name__ == "__main__": block,

# 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]

已添加到 多路复用.py.

这篇关于sockjs - 实现房间的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:21