【问题标题】:socket.emit doesn't working with flasksocket.emit 不适用于烧瓶
【发布时间】:2018-08-28 18:48:18
【问题描述】:

我尝试用烧瓶作为项目制作简单的聊天应用程序,所以我有页面
当用户创建新的聊天室时,聊天室会显示给所有其他用户
所以我有这个代码,但是 socket.emit 不能用它
我的意思是烧瓶应用程序没有收到数据这里是代码

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") 以了解应用程序是否收到数据但没有
该函数根本没有调用为什么

【问题讨论】:

    标签: socket.io flask-socketio


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 2016-01-08
      • 2018-03-17
      • 1970-01-01
      相关资源
      最近更新 更多