gpt4 book ai didi

javascript - 组名必须是仅包含 ASCII 字母数字、连字符或句点的有效 Unicode 字符串

转载 作者:行者123 更新时间:2023-12-01 04:42:28 25 4
gpt4 key购买 nike

我在创建聊天室方面需要帮助。它的工作方式就像我正在进入一个特定的房间(如“Python 社区”、“DjangoDev”等)。
我使用的软件包:

  • Django==1.9.7
  • channel ==1.1.8
  • asgi-redis==1.4.3

  • 我使用组名的 slug 字段。我在 Channels 中过滤这个 slug 找到一个组并调用 save每次有新消息出现。
    但是 Channels 抛出一个错误,指出组的名称无效:
    (venv) alibek@OverlorD:~/Desktop/my_porject/RedProject$ ./manage.py runworker
    2018-03-17 13:59:51,607 - INFO - runworker - Using single-threaded worker.
    2018-03-17 13:59:51,608 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer)
    2018-03-17 13:59:51,608 - INFO - worker - Listening on channels chat-messages, http.request, websocket.connect, websocket.disconnect, websocket.receive
    Not Found: /home/
    Not Found: /favicon.ico
    Traceback (most recent call last):
    File "./manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/channels/management/commands/runworker.py", line 83, in handle
    worker.run()
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/channels/worker.py", line 151, in run
    consumer_finished.send(sender=self.__class__)
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 192, in send
    response = receiver(signal=self, sender=sender, **named)
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/channels/message.py", line 105, in send_and_flush
    sender.send(message, immediately=True)
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/channels/channel.py", line 88, in send
    self.channel_layer.send_group(self.name, content)
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/asgi_redis/core.py", line 289, in send_group
    assert self.valid_group_name(group), "Group name not valid"
    File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/asgiref/base_layer.py", line 122, in valid_group_name
    raise TypeError("Group name must be a valid unicode string containing only ASCII alphanumerics, hyphens, or periods.")
    TypeError: Group name must be a valid unicode string containing only ASCII alphanumerics, hyphens, or periods.
    后端代码:
    def chat_room(request, room_name_url):
    room = get_object_or_404(ChatRoom, slug=room_name_url)
    return render(request, 'chat_room.html', {'room': room})

    def msg_consumer(message):
    # Save to model
    room_slug = message.content['room']
    room = ChatRoom.objects.filter(slug=room_slug)

    # Broadcast to listening sockets
    Group("chat-%s" % room).send({
    "text": message.content['message'],
    })

    # Connected to websocket.connect
    @channel_session
    @enforce_ordering
    def connect(message):

    message.reply_channel.send({"accept": True})
    # Url of room e.g localhost:8000/chat/django
    path = urlsplit(message.content['path'])
    # Returns slug field of room from url, i.e. 'django'
    room = path[2].strip("/chat/").decode('utf-8')
    # Save room in session and add us to the group
    message.channel_session['room'] = room
    message.content['room'] = room
    Group("chat-%s" % room).add(message.reply_channel)

    # Connected to websocket.receive
    @channel_session
    @enforce_ordering
    def receive(message):
    # Stick the message onto the processing queue
    Channel("chat-messages").send({
    "room": message.channel_session['room'],
    "message": message['text'],
    })

    # Connected to websocket.disconnect
    @channel_session
    @enforce_ordering
    def disconnect(message):
    Group("chat-%s" % message.channel_session['room']).discard(message.reply_channel)
    前端代码:
    var messages = document.getElementById("messages");
    var text = document.getElementById("text");
    var button = document.getElementById("send");

    var ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
    var socket = new ReconnectingWebSocket(
    ws_scheme
    + '://'
    + window.location.host
    + window.location.pathname
    );
    socket.onmessage = function (e) {
    var message = document.createElement("p");
    var data = JSON.parse(e.data);
    var noQuotes = data.msg.split('"').join('');
    var user = data.user;
    message.innerHTML = user + ': ' + noQuotes;
    console.log(JSON.parse(e.data));
    messages.appendChild(message);
    };
    button.addEventListener("click", function (event) {
    event.preventDefault();
    var data_to_server = JSON.stringify(text.value);
    socket.send(data_to_server);
    text.value = '';
    text.focus();
    });
    另外,我不喜欢使用 session ,Django 第一次进入应用程序时会抛出错误。

    最佳答案

    嗯,我找到了麻烦。我使用了一个对象而不是一个字符串:

    room = ChatRoom.objects.filter(slug=room_slug)

    # Broadcast to listening sockets
    Group("chat-%s" % room).send({...
    解决方案:
    Group("chat-%s" % room_slug).send({...

    关于javascript - 组名必须是仅包含 ASCII 字母数字、连字符或句点的有效 Unicode 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49333975/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com