Я использую функции на основе классов для каналов Django, и я получаю эту ошибку, когда пытаюсь получить response_channel клиента, который соединяется с websocket_connect.
Я относительно новичок в каналах
Consumer.py
import asyncio
import json
from django.contrib.auth import get_user_model
from channels.consumer import AsyncConsumer
from channels.db import database_sync_to_async
from .models import Thread, ChatMessage
class ProgramConsumer(AsyncConsumer):
async def websocket_connect(self, message):
print("connected", message)
#What does this do??? Is it accepting the connection that is sent from the client html page
await self.send({
"type": "websocket.accept"
}) #Execute that code and wait
await self.reply_channel.send({
"text": json.dumps({
"action": "reply_channel",
"reply_channel": message.reply_channel.name,
})
}
)
Я пытаюсь повторить код ниже, но на основе классовфункции.
@channel_session
def ws_connect(message):
message.reply_channel.send({
"text": json.dumps({
"action": "reply_channel",
"reply_channel": message.reply_channel.name,
})
})
Ниже приведен JQuery на моей странице HTML
JQuery
$(function() {
console.log(window.location)
var loc = window.location
//var formData = $("#form") //Grabs the form element above
//var msgInput = $("#id_message")
//var chatHolder = $("#chat-items")
//var me = $("#myUsername").val()
var wsStart = 'ws://'
if (loc.protocol == 'https:'){
wsStart = 'wss://'
}
// Define endpoint
var endpoint = wsStart + loc.host + loc.pathname
// methods that come in the socket
console.log("Connecting to " + endpoint)
// Use endpoint
var socket = new WebSocket(endpoint)
// RECIEVING
socket.onmessage = function(message) {
console.log("Got message: " + message.data);
var data = JSON.parse(message.data);
// if action is started, add new item to table
if (data.action == "started") {
var task_status = $("#task_status");
var ele = $('<tr></tr>');
ele.attr("id", data.program_id);
var item_id = $("<td></td>").text(data.program_id);
ele.append(item_id);
var item_name = $("<td></td>").text(data.program_name);
ele.append(item_name);
var item_status = $("<td></td>");
item_status.attr("id", "item-status-"+data.program_id);
var span = $('<span class="label label-primary"></span>').text(data.program_status);
item_status.append(span);
ele.append(item_status);
task_status.append(ele);
}
// if action is completed, just update the status
else if (data.action == "completed"){
var item = $('#item-status-' + data.program_id + ' span');
item.attr("class", "label label-success");
item.text(data.program_status);
}
};
// SENDING
// Takes form data and stringifys it inso JSON action and program_name
$("#taskform").on("submit", function(event) {
var message = {
action: "start_sec3",
program_name: $('#task_name').val()
};
socket.send(JSON.stringify(message));
$("#task_name").val('').focus();
return false;
});
});