django频道中有多个websocket.receive

时间:2016-04-21 06:26:30

标签: python django websocket python-3.4

由于我是websockets的新手,是否有可能为不同消费者群体创建多个websocket.receive路由?

app/consumers.py
# First receiver
def ws_receive1(message):
    Group(
       'Group1',
       channel_layer=message.channel_layer
       ).send({'text': "receiver1"})

def ws_receive2(message):
    Group(
       'Group2',
       channel_layer=message.channel_layer
       ).send({'text': "receiver2"})

# routing.py
channel_routing = [
    route("websocket.connect", consumers.ws_connect1),
    route("websocket.disconnect", consumers.ws_disconnect1),
    route("websocket.receive", consumers.ws_receive1),

    route("websocket.connect", consumers.ws_connect2),
    route("websocket.disconnect", consumers.ws_disconnect2),
    route("websocket.receive", consumers.ws_receive2),
]

有关它的任何建议吗?

1 个答案:

答案 0 :(得分:2)

这是我对上述内容的实现。如果需要进一步修改,请建议。

#app1/routing.py

from channels.routing import route
from . import consumers

post_websocket = [
    route("websocket.connect", consumers.websocket_connect),
    route("websocket.disconnect", consumers.websocket_disconnect)
]

vote_websocket = [
    route("websocket.connect", consumers.websocket_voteconnect),
    route("websocket.receive", consumers.ws_updatevotes),
    route("websocket.disconnect", consumers.websocket_votedisconnect)
]


# myproject/routing.py
from channels import include

channel_routing = [
    include("app1.routing.post_websocket", path=r"^/app1/post/notification"),
    include("app1.routing.vote_websocket", path=r"^/app1/vote/notification"),
]

您可以在asifpy/channels-examples

找到代码