2016-10-12 3 views
0

Node jssocketと覚えています。私はサンプルプログラムを持っていますHereSocket.ioが放射して動作していない

実行しようとすると、エラーは表示されませんが、何も起こっていません。

socket.emitsocket.ioがトリガーしていないことを確認しました。あなたはそれが現在では、またはそうでなければある機能の

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); 
      }); 
     }); 

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

実行されません(そのコードは、コメントがクリックされたときにのみ実行されます)。

例えば、

$("#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

があります
関連する問題