是否可以分配django渠道组?

时间:2017-10-21 20:57:21

标签: python django websocket channels

我希望你没事:

我的问题是我是否可以建立从客户端到特定组的Web套接字的连接, 我目前正在按如下方式联系客户:

//connecting client with js
var ws_scheme = window. location. protocol == "https:"? "wss":"ws";
//var ws_path = ws_scheme +': //' + window. location. host + "/sync/";
var ws_path = ws_scheme + ": //localhost: 8001";

console. log ("Connecting to " + ws_path);
var socket = new ReconnectingWebSocket (ws_path);

好的,这是怎么回事, 问题是我希望每个客户端都连接到一个组 先前创建的例子:

def ws_connect (message):
  for x in users:
    Group (x). add (message. reply_channel)

因此将消息发送给相应的组

Group ("group1"). send ({' text': json. dumps (msg)})
Group ("group2"). send ({' text': json. dumps (msg)})

1 个答案:

答案 0 :(得分:1)

连接到服务器时,

在url中传递组名。

  

WSS://本地主机:8001 /同步/< groupname> /

并且路由将是

route("websocket.connect", ws_connect, path=r"^/sync/(?P<group_name>[a-zA-Z0-9_]+)/$")

和consumer.py

def ws_connect(message, group_name):
    Group(group_name).add(message.reply_channel)