socket.emit不适用于烧瓶

时间:2018-08-28 18:48:18

标签: socket.io flask-socketio

我尝试使用flask作为项目制作简单的聊天应用程序,所以我有了页面
当用户建立新的聊天室时,该聊天室会出现在所有其他用户中
所以我有这段代码,但是socket.emit不能正常工作
我的意思是Flask应用程序没有收到数据,这里是代码

document.addEventListener('DOMContentLoaded',function  () {

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


// When connected, configure buttons
socket.on('connect', function () {



    document.querySelector("#create").onclick=function (){

        const new_channel=document.querySelector("#new_chanell").value;
        const channels_list=document.querySelector("#channels");
        for (let i=0; i<channels_list.length;i++){
            if(channels_list[i].value==new_chanell){

                document.querySelector("h1").innerHTML="this channel allready here";
                return false;}
        }
        socket.emit('add_channel', {'channel': new_channel});
        document.querySelector("h1").innerHTML="new channel";


        return false;

}});

在烧瓶中

@socketio.on("add_channel")
def add_channel(data):
raise("he")
channel=data["channel"]
channel_list.append(channel)
emit("new_one",{"channel":channel},broadcast=True)

所以我放了raise(“ he”)来知道应用程序是否接收到数据,但没有接收到
该功能根本没有调用

1 个答案:

答案 0 :(得分:1)

在连接URL上,您正在传递/channels命名空间。如果是这样,那么在服务器上,您必须将名称空间添加到所有事件处理程序中:

@socketio.on("add_channel", namespace='/channels')
def add_channel(data):
    # ...
相关问题