Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript &引用;“线程中的异常”;更新Flask应用中的键值对时出错_Javascript_Python_Flask_Socket.io_Flask Socketio - Fatal编程技术网

Javascript &引用;“线程中的异常”;更新Flask应用中的键值对时出错

Javascript &引用;“线程中的异常”;更新Flask应用中的键值对时出错,javascript,python,flask,socket.io,flask-socketio,Javascript,Python,Flask,Socket.io,Flask Socketio,我正在构建一个基本的聊天程序(来自CS50s网络编程的Flack) 我有一个字典,我把频道和消息存储为键值对 消息位于列表中,因此一个键值对看起来像: {“channelExample”:[“msg1”、“msg2”]} 我还有另一个变量,它跟踪用户正在发送消息的当前房间/频道,名为currentRoom 当用户提交消息时,我正试图通过执行以下操作来更新该通道中的消息(emit已导入&我已确认currentRoom&输入消息为字符串值): @socketio.on(“提交消息”) def sub

我正在构建一个基本的聊天程序(来自CS50s网络编程的Flack)

我有一个字典,我把频道和消息存储为键值对

消息位于列表中,因此一个键值对看起来像:

{“channelExample”:[“msg1”、“msg2”]}

我还有另一个变量,它跟踪用户正在发送消息的当前房间/频道,名为currentRoom

当用户提交消息时,我正试图通过执行以下操作来更新该通道中的消息(emit已导入&我已确认currentRoom&输入消息为字符串值):

@socketio.on(“提交消息”)
def submitMessage(消息):
频道[currentRoom]。追加(消息)
发出(“显示消息”,消息)
但是,
频道[currentRoom]的线程中出现了一个“异常…”错误。追加(消息)
&我不确定原因

我在烧瓶中的完整代码:

导入操作系统
从flask导入flask、会话、呈现模板、url、请求、闪存、重定向、jsonify
从flask_socketio导入socketio,发送,发射,加入房间,离开房间
app=烧瓶(名称)
app.config[“SECRET\u KEY”]=os.getenv(“SECRET\u KEY”)
socketio=socketio(应用程序)
当前房间=无
通道={}
@附件路线(“/”)
def index():
返回render_模板(“welcome.html”,channels=channels)
@socketio.on(“新频道”)
def新频道(频道名称):
#存储新频道以跟踪它
channels.update({channelName:[]})
@socketio.on(“检索频道”)
def RetrieveChannel():
channelNames=[]
对于通道中的通道:
channelNames.append(频道)
发射(“提供通道”,通道名称)
@socketio.on(“检索消息”)
def加载消息(通道名称):
currentRoom=channelName
channelMessages=频道[currentRoom]
发出(“加载消息”,通道消息)
@socketio.on(“提交消息”)
def submitMessage(消息):
频道[currentRoom]。追加(消息)
发出(“显示消息”,消息)
Javascript:

document.addEventListener('DOMContentLoaded', () => {

    // Connect to websocket
    var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port);

    // When connected, 
    socket.on('connect', () => {

        var nameInput = document.querySelector("#usernameInput");
        var welcomeMessage = document.querySelector("#welcomeMessage");
        var createChannel = document.querySelector("#createChannel");
        var newChannelForm = document.querySelector("#newChannelForm");
        var newMessageForm = document.querySelector("#newMessageForm");

        function userExists() {
            // Check if user has come here before
            if (localStorage.getItem("username")) {
                // Display a welcome message
                welcomeMessage.innerHTML = `Welcome back ${localStorage.getItem("username")}!`;
                nameInput.style.display = "none";
                return true;
            }
            else {
                return false;
            }
        };

        function createChannelBtn(name) {
            // Create new channel & style it
            let newChannel = document.createElement("button");
            newChannel.id = name;
            newChannel.innerHTML = name;
            newChannel.className = "btn btn-block btn-outline-dark";
            newChannel.style.display = "block";

            // Attach to current list 
            document.querySelector("#channels").appendChild(newChannel);

            // When someone clicks the channel
            newChannel.onclick = () => {

                newChannel.classList.toggle("active");

                socket.emit("retrieve messages", newChannel.id);
                console.log("Retrieving messages!!!");

                socket.on("load messages", channelMessages => {
                    console.log("loading messages!");
                    for (let i = 0; i < channelMessages.length; i++) {
                        createMessage(channelMessages[i]);
                    }
                });

            };
        };

        function createMessage(messageContent) {
            let message = document.createElement("h6");
            message.innerHTML = messageContent;
            document.querySelector("#messageWindow").appendChild(message);
            console.log("Currently creating message!");
        };

        function loadChannels() {
            socket.emit("retrieve channels")

            socket.on("providing channels", channelNames => {
                for (let i = 0; i < channelNames.length; i++) {
                    createChannelBtn(channelNames[i]);
                }
            });
        };

        // Make sure the new channel form is not displayed until "Create channel" button is clicked
        newChannelForm.style.display = "none";

        // Check if user exists already in local storage
        userExists();

        loadChannels();

        // If someone submits a username...
        nameInput.addEventListener("click", () => {
            // if that username exists, do nothing
            if (userExists()) {
            }
            // else remember the username
            else {
                localStorage.setItem("username", document.querySelector("#user").value);
            }
        });

        // When someone wants to create a channel
        createChannel.addEventListener("click", () => {
            // Show form
            newChannelForm.style.display = "block";

            // When user inputs new channel name...
            newChannelForm.onsubmit = () => {

                // Retrieve their input
                var newChannelName = document.querySelector("#newChannel").value;

                // Create a new channel
                createChannelBtn(newChannelName);

                // Notify server to store new channel 
                socket.emit("new channel", newChannelName);

                // Clear input field
                document.querySelector("#newChannel").innerHTML = "";

                return false;

            };
        });

        newMessageForm.onsubmit = () => {
            let message = document.querySelector("#newMessage").value;
            console.log("You have entered " + message);

            socket.emit("submit message", message);
            console.log("Submitted message!");

            socket.on("display message", message => {
                createMessage(message);
                console.log("Displaying message!!");
            });

            return false;
        };

    });

    // DOM Ending Bracket
});

document.addEventListener('DOMContentLoaded',()=>{
//连接到websocket
var socket=io.connect(location.protocol+'/'+document.domain+':'+location.port);
//当连接时,
socket.on('connect',()=>{
var nameInput=document.querySelector(“#usernameInput”);
var welcomeMessage=document.querySelector(“welcomeMessage”);
var createChannel=document.querySelector(“createChannel”);
var newChannelForm=document.querySelector(“newChannelForm”);
var newMessageForm=document.querySelector(“newMessageForm”);
函数userExists(){
//检查用户以前是否来过这里
if(localStorage.getItem(“用户名”)){
//显示欢迎信息
welcomeMessage.innerHTML=`欢迎回来${localStorage.getItem(“用户名”)}!`;
nameInput.style.display=“无”;
返回true;
}
否则{
返回false;
}
};
函数createChannelBtn(名称){
//创建新频道并设置其样式
让newChannel=document.createElement(“按钮”);
newChannel.id=名称;
newChannel.innerHTML=名称;
newChannel.className=“btn btn块btn轮廓暗”;
newChannel.style.display=“block”;
//附加到当前列表
document.querySelector(“#channels”).appendChild(newChannel);
//当有人点击频道时
newChannel.onclick=()=>{
newChannel.classList.toggle(“活动”);
emit(“检索消息”,newChannel.id);
log(“检索消息!!!”;
socket.on(“加载消息”,channelMessages=>{
log(“加载消息!”);
for(设i=0;i{
for(设i=0;i{
//如果该用户名存在,则不执行任何操作
如果(userExists()){
}
//还记得用户名吗
否则{
localStorage.setItem(“用户名”,document.querySelector(“用户”).value);
}
});
//当有人想要创建频道时
createChannel.addEventListener(“单击”,()=>{
//表演形式
newChannelForm.style.display=“block”;
//当用户输入新频道名称时。。。
newChannelForm.onsubmit=()=>{
//检索他们的输入
var newChannelName=document.querySelector(“#newChannel”).value;
//创建一个新频道
createChannelBtn(newChannelName);
//通知ser