Websocket身份验证的最佳实践 - Django频道

时间:2017-07-04 14:03:26

标签: django websocket django-channels

是否已准备好为websocket客户端使用Django Channels身份验证方法?

到目前为止我做了以下操作,客户端正常通过websocket连接,他应该发送的第一条消息是一条身份验证消息:

导入json 来自渠道导入集团

def login(params, message):

    print "login"

    if "Username" in params and "Password" in params and "ApplicationName" in params:

        if params["Username"] == "dummy" and params["Password"] == "1234" and params["ApplicationName"] != "":

            message.reply_channel.send({"text":json.dumps({"authentification":"succeeded"})})

            return


    message.reply_channel.send({"text":json.dumps({"authentification":"unsucceeded"})})
    message.reply_channel.send({"close": True})

routing.py

from . import consumers
from channels import route

channel_routing = [
    route('websocket.connect', consumers.ws_connect),
    route('websocket.receive', consumers.ws_receive),
    route('websocket.disconnect', consumers.ws_disconnect),
]

consumers.py

@channel_session
def ws_receive(message):

    text = message.content.get('text')
    dict = json.loads(text)

    if "connect" in dict:

        login(dict["connect"], message)

0 个答案:

没有答案