Socket.io发出并且.on无法正常工作

时间:2016-10-12 10:06:25

标签: node.js express socket.io gruntjs

我正在使用Node js学习socket。我从Here

获得了一个示例程序

当我尝试运行它时,它没有显示错误,但也没有发生任何事情。

我试过并发现socket.emitsocket.io没有触发。

客户端

$("#addComment").click(function(event){
            var userName = $("#name").val();
            var userComment = $("#comment").text();
            if(userName === "" || userComment === "") {
                alert("Please fill the form.");
                return;
            }
            socket.emit('comment_added',{user : userName, comment : userComment});
            socket.on('notify_everyone',function(msg){
                notifyMe(msg.user,msg.comment);
            });
        });

function notifyMe(user,message) {
        // Let's check if the browser supports notifications
        if (!("Notification" in window)) {
            alert("This browser does not support desktop notification");
        }
        // Let's check if the user is okay to get some notification
        else if (Notification.permission === "granted") {
            // If it's okay let's create a notification
            var options = {
                body: user + "Posted a comment" + message,
                dir : "ltr"
            };
            var notification = new Notification("Hi there",options);
        }
        // Otherwise, we need to ask the user for permission
        // Note, Chrome does not implement the permission static property
        // So we have to check for NOT 'denied' instead of 'default'
        else if (Notification.permission !== 'denied') {
            Notification.requestPermission(function (permission) {
                // Whatever the user answers, we make sure we store the information
                if (!('permission' in Notification)) {
                    Notification.permission = permission;
                }
                // If the user is okay, let's create a notification
                if (permission === "granted") {
                    var options = {
                        body: user + "Posted a comment" + message,
                        dir : "ltr"
                    };
                    var notification = new Notification("New comment added.",options);
                }
            });
        }
        // At last, if the user already denied any notification, and you
        // want to be respectful there is no need to bother them any more.
    }

服务器端

io.on('connection',function(socket){
console.log('We have user connected !');
io.sockets.on('comment_added',function(data){
    console.log(data);
    db.addComment(data.user,data.comment,mysql,pool,function(error,result){
        if (error) {
            io.emit('error');
        } else {
            socket.broadcast.emit("notify_everyone",{user : data.user,comment : data.comment});
        }
    });
});
});

1 个答案:

答案 0 :(得分:1)

你需要把

socket.on('notify_everyone',function(msg){
notifyMe(msg.user,msg.comment);
});

在它当前所在的函数之外,否则它将无法运行(该代码仅在单击注释时运行)。

例如,

$("#addComment").click(function(event){
    var userName = $("#name").val();
    var userComment = $("#comment").text();
    if(userName === "" || userComment === "") {
        alert("Please fill the form.");
        return;
    }
    socket.emit('comment_added',{user : userName, comment : userComment});
    });
socket.on('notify_everyone',function(msg){
    notifyMe(msg.user,msg.comment);
});

另外,有关于socket.io website以及chat app tutorial

的文档