1
有一個客戶端和服務器端代碼:回調函數socket.io + Node.js的
客戶:
socket.on('connect', function() {
showSystemMessage('Connected.');
socket.emit('setuser', wm, function(data){
console.log(data);
socket.emit('whoisonline', wm, function(data){
getOnlineFriends(data);
});
});
});
服務器:
io.on('connection', function(socket) {
socket.on('setuser', function (data, cb) {
cb(data);
});
});
我得到錯誤:
TypeError: undefined is not a function on line 18
第18行:
socket.on('setuser', function (data, cb) {
cb(data); // Line 18
});
然後我想你的解決方案,我得到:
TypeError: undefined is not a function in line 35
socket.on('whoisonline', function (data, cb) {
redis.sinter('user.friend:' + data.id, 'onlineusers', function(error, intersection) {
friends.online = intersection;
friends.total = intersection.length;
if(friends.total > 0){
intersection.forEach(function(entry) {
var socketid = clients[entry];
io.sockets.connected[socketid].emit('in', { id : data.id, total : friends.total });
});
}
cb(friends); // Line 35
});
});
嗯,謝謝你,似乎工作,但我得到回調againn重寫後在你的代碼上。編輯問題 – AhmedFaud 2014-11-06 15:23:37
@AhmedFaud用'socket.emit(「whoisonline」,friends)替換第35行;'' – 2014-11-06 15:35:58