是否可以在连接中分配 Django 频道组?

Is it possible to assign django channels group in connection?

希望你一切都好:

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

//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)})

连接到服务器时,在 url 中传递组名。

wss://localhost:8001/sync/< groupname >/

路由将是

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

并在 consumers.py

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