socket.emit 不适用于烧瓶



我尝试使用 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("他"(来知道应用程序是否接收数据但没有
函数根本不调用为什么

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

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

最新更新