【问题标题】:Is it possible to assign django channels group in connection?是否可以在连接中分配 django 频道组?
【发布时间】:2018-04-02 17:46:53
【问题描述】:

希望你没事:

我的问题是我是否可以从客户端连接到特定组的 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)})

【问题讨论】:

    标签: python django websocket channels


    【解决方案1】:

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

    wss://localhost:8001/sync//

    路由将是

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-10-25
      • 1970-01-01
      • 2018-07-28
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      相关资源
      最近更新 更多