首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >socket.emit不能与flask一起工作

socket.emit不能与flask一起工作
EN

Stack Overflow用户
提问于 2018-08-29 02:48:18
回答 1查看 353关注 0票数 1

我试着使简单的聊天应用程序与烧瓶作为项目,所以我有页面

当用户创建新聊天室时,聊天室将显示给所有其他用户

所以我有这段代码,但是socket.emit不能在上面工作

我的意思是flask应用程序不能接收数据这里是代码

代码语言:javascript
复制
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;

}});

在烧瓶中

代码语言:javascript
复制
@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")来知道应用程序是否接收到了数据,但没有

为什么这个函数根本不调用

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-29 06:29:03

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

代码语言:javascript
复制
@socketio.on("add_channel", namespace='/channels')
def add_channel(data):
    # ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52064500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档