2017-10-21 79 views
1

我希望你沒事:是否可以在連接中分配django頻道組?

我的問題是我是否能對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

連接到服務器時,在URL中傳遞羣組名稱。

WSS://本地主機:8001 /同步/ <組名>/

和路由將是

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) 
+0

謝謝拉胡爾 – NEFEGAGO

相關問題