2013-05-05 80 views
1

作为标题是obvoius我需要发送一些错误信息给未经授权的用户,我需要知道如何实现这个例如我需要发送此消息给用户需要发送错误给未经授权的用户在socket.io

你不要有任何的用户名,开始聊天

,并打印在用户的浏览器,我应该怎么做呢?客户端代码是这样的

//this is the client side code 
     var socket = io.connect('http://localhost', { resource: '/chat/app.js' }); 

     // on connection to server, ask for user's name with an anonymous callback 
     socket.on('connect', function(){ 
      // call the server-side function 'adduser' and send one parameter (value of prompt) 
      socket.emit('adduser') 
     }); 

    socket.socket.on('error', function (reason){ 
     console.log('Unable to connect Socket.IO', reason); 
    }); 

但原因我在控制台得到的是

无法连接Socket.IO握手错误

我应该如何打印邮件这是用户的原因是没有得到授权?

这是服务器端代码

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

     io.configure(function(){ 
      io.set('authorization', function (handshakeData, callback) { 
      // findDatabyip is an async example function 
      findDatabyIP(handshakeData.address.address, function (err, data) { 
       if (err) return callback(err); 

       if (data.authorized) { 
       handshakeData.foo = 'bar'; 
       for(var prop in data) handshakeData[prop] = data[prop]; 
       callback(null, true); 
       } else { 
//THIS IS THE MESSAGE ********************************************* 
       callback('you dont have any username to begin chat', false); 
       } 
      }) 
      }); 
     }); 
+0

你还得到这个错误从findDatabyIP回调日期匹配时?换句话说,你是否总是得到这个错误,或者只有当你的用户在findDatabyIP调用中找不到时? – 2013-05-06 18:44:37

回答

0

要发送的出错信息给用户,你必须修改manager.js误差函数(socket.io \ LIB \ manager.js;线768约)从这个

function error (err) { 
    writeErr(500, 'handshake error'); 
    self.log.warn('handshake error ' + err); 
    }; 

这个

function error (err) { 
    writeErr(500, /*'handshake error'*/ err); 
    self.log.warn('handshake error ' + err); 
    }; 
相关问题