节点聊天给了 throw er; //未处理的错误事件

Posted

技术标签:

【中文标题】节点聊天给了 throw er; //未处理的错误事件【英文标题】:Node chat gives throw er; //unhandled error event 【发布时间】:2015-03-11 18:00:15 【问题描述】:

于是我建立了一个小型的基本聊天系统:

服务器代码:

    var io = require('socket.io').listen(8000);

// open the socket connection
io.sockets.on('connection', function (socket) 

   // listen for the chat even. and will recieve
   // data from the sender.
   socket.on('chat', function (data) 

      // default value of the name of the sender.
      var sender = 'unregistered';

      // get the name of the sender
      var name = socket.nickname;
         console.log('Chat message by ', name);
         console.log('error ', err);
         sender = name;


      // broadcast data recieved from the sender
      // to others who are connected, but not 
      // from the original sender.
      socket.broadcast.emit('chat', 
         msg : data, 
         msgr : sender
      );
   );

   // listen for user registrations
   // then set the socket nickname to 
   socket.on('register', function (name) 

      // make a nickname paramater for this socket
      // and then set its value to the name recieved
      // from the register even above. and then run
      // the function that follows inside it.
      socket.nickname = name;

         // this kind of emit will send to all! :D
         io.sockets.emit('chat', 
            msg : "naay nag apil2! si " + name + '!', 
            msgr : "mr. server"
      );
   );

);

客户端:

<html>
   <head>
      <script src="http://localhost:8000/socket.io/socket.io.js"></script>
      <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
      <script>
         var name = '';
         var socket = io.connect('http://localhost:8000');

         // at document read (runs only ones).
         $(document).ready(function()
            // on click of the button (jquery thing)
            // the things inside this clause happen only when 
            // the button is clicked.
            $("button").click(function()

               // just some simple logging
               $("p#log").html('sent message: ' + $("input#msg").val());

               // send message on inputbox to server
               socket.emit('chat', $("input#msg").val() );

               // the server will recieve the message, 
               // then maybe do some processing, then it will 
               // broadcast it again. however, it will not 
               // send it to the original sender. the sender
               // will be the browser that sends the msg. 
               // other browsers listening to the server will
               // recieve the emitted message. therefore we will
               // need to manually print this msg for the sender.
               $("p#data_recieved").append("<br />\r\n" + name + ': ' + $("input#msg").val());

               // then we empty the text on the input box.
               $("input#msg").val('');
            );

            // ask for the name of the user, ask again if no name.
            while (name == '') 
               name = prompt("What's your name?","");
            

            // send the name to the server, and the server's 
            // register wait will recieve this.
            socket.emit('register', name );
         );

         // listen for chat event and recieve data
         socket.on('chat', function (data) 

            // print data (jquery thing)
            $("p#data_recieved").append("<br />\r\n" + data.msgr + ': ' + data.msg);

            // we log this event for fun :D
            $("p#log").html('got message: ' + data.msg);

         );
      </script>
   </head>
   <body>
      <input type="text" id="msg"></input><button>Click me</button>
      <p id="log"></p>
      <p id="data_recieved"></p>
   </body>
</html>

虽然它工作得很好,但每次我从客户端向服务器传递一些东西时,我都会得到: "throw er; // 未处理的 'error' 事件。

ReferenceError: err 未定义。

【问题讨论】:

【参考方案1】:

你需要监听错误事件,比如

socket.on('error', function () );

https://github.com/Automattic/socket.io/wiki/exposed-events

您正在调用console.log('error ', err),但“err”未在任何地方定义。

【讨论】:

嗯,你说得对。但即使我定义了它,它仍然会使服务器崩溃。 您是否使用socket.on('error', function () ); 来捕获错误? 对我来说socket.on('error', function () ); 也不起作用。我已将“错误”重命名为其他名称,现在它可以正常工作了。 是的,它工作正常我只是把这个方法放在回调函数中用于事件连接

以上是关于节点聊天给了 throw er; //未处理的错误事件的主要内容,如果未能解决你的问题,请参考以下文章

节点 events.js:174 throw er; // 未处理的“错误”事件

Nodejs 得到 throw er; // 未处理的“错误”事件 [重复]

NodeJS 错误 events.js:182 throw er; // 未处理的“错误”事件 ^ 错误:监听 EADDRINUSE :::3000 [重复]

event.js:174 throw er// 未处理的“错误”事件操作不允许

为啥 npm start 会抛出 events.js:187 throw er; // 我的反应项目中未处理的“错误”事件?

events.js:160 抛出 er; // 未处理的“错误”事件 ^