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);
}
})
});
});
你還得到這個錯誤從findDatabyIP回調日期匹配時?換句話說,你是否總是得到這個錯誤,或者只有當你的用戶在findDatabyIP調用中找不到時? – 2013-05-06 18:44:37