2014-10-10 37 views
5

以下特定用戶是在前端的代碼,其中包含storeSelUserId USER_ID發送MESSAGE-發送更新通知使用socket.io

FYI - 節點版本1.1.0

// Socket Notification 
var socket = io('http://localhost:6868'); 
socket.on('connection', function (data) { 
    socket.emit('send notification', { sent_to: storeSelUserId }); 
}); 

以下是路線文件服務器代碼 -

var clients = {}; 
io.on('connection', function (socket) { 
    socket.emit('connection', "Connection Created."); 
    socket.on('send notification', function (sent_to) { 
    console.log(sent_to); 
    }); 
}); 

在控制檯sent_to正顯示出數組user_id

現在正在啓動socket.io我堅持解決方案,我該如何發送消息到這些特定的用戶ID。

我搜索,發現我需要按每個用戶的插座,所以我就改到 -

var users = []; 
io.on('connection', function (socket) { 
    users.push({socket_id: socket.id}); 
    socket.emit('connection', "Connection Created."); 
    socket.on('send notification', function (sent_to) { 
    console.log(sent_to); 
    }); 
}); 

但我在左右爲難的是別人做我需要做什麼來存儲它user_id指socket_id,然後使用該特定ID更新用戶的div?

EDIT -

添加控制器 - (前端)

其中創建備忘錄和發送給特定用戶

var socket = io('http://localhost:6868'); 
socket.on('connection', function (data) { 
    socket.emit('send memo notification', {creator_id: creator_id, sent_to: [Array of user_ids to whom memo to send]}); 
}); 

控制板控制器前端接口 - (前端)

前端接口,其中通知計數,以顯示 「notificationCount」

if (SessionService.currentUser._id) { 
    var socket = io('http://localhost:6868'); 
    socket.on('connection', function (data) { 
     socket.emit('get notifications', {user_id: SessionService.currentUser._id}); 
    }); 

    socket.on('notification data', function(data){ 
     console.log("-- Not Data Test -"); 
     $scope.notificationCount = data.length; 
    }); 
} 

代碼在服務器端 -

io.on('connection', function (socket) { 

    socket.emit('connection', "Connection Created."); 

    socket.on('send memo notification', function(data) { 
     notifications.createNotification(data); 
    }); 

    socket.on('get notifications', function(data){ 
    notifications.getNotifications(data, function(response){ 
     socket.emit('notification data', response.data); 
    }); 
    }); 

}); 

後端控制器代碼 -

exports.getNotifications = function(data, callback) { 
    var userId = data.user_id; 
    Notification.find({receiver_id: userId}, function(err, response){ 
     if (err) 
      callback({"message": "error", "data": err, "status_code": "500"}); 
     else 
      callback({"message": "success", "data": response, "status_code": "200"}); 
    }); 
}; 

exports.createNotification = function(data) { 
    var notificationData = data; 
    var x = 0; 
    for(var i=0; i< notificationData.length; i++) { 
     // Code 
     Notification(notificationData[i]).save(function(err,response){ 
      if (err) 
       return false; 
     });  
     if (x === notificationData.length - 1) { 
      return true; 
     } 
     x++; 
    } 
}; 

回答

18

如果你想使用你自己的用戶ID,那麼就沒有辦法將套接字ID映射到用戶ID。我假設一個客戶端從某個地方知道它的用戶標識,所以它可以在連接後將其用戶標識發送到服務器。

客戶

socket.on('connection', function (data) { 
    socket.emit('setUserId', myUserId); 
}); 

服務器保存每個用戶ID的插座。

socket.on('setUserId', function (userId) { 
    users[userId]=socket; 
}); 

如果您在服務器中有這樣的映射,您可以使用用戶標識僅向該客戶端發送消息。

socket.on('send notification', function (userId) { 
    users[userId].emit('notification', "important notification message"); 
}); 

編輯:直接保存對應的套接字甚至更好。

+1

請找我edit..I我不是在客戶端side..I獲得自動更新已經刷新瀏覽器則只更新反映 – Trialcoder 2014-10-13 12:15:47

+0

讓我知道,如果有什麼東西還不清楚 – Trialcoder 2014-10-13 12:19:01

3

據我所知,你需要私人通知發送給只有一些用戶。爲此,請將您的用戶名稱保存到您希望發送的用戶名稱及其對應的套接字中。

username [socket.name] = username to be added; 
usersocket [ socket.name ] =socket; 

然後發出消息到只有該用戶,使用

usersocket[ socket.name ].emit('event for send message', ' what you want to send '); 
+0

如果你願意,我可以發送代碼。 – 2014-10-13 08:54:51

+0

請查看我的編輯 – Trialcoder 2014-10-13 11:29:51

+0

你能轉發更新以便我可以查找嗎? – 2014-10-14 06:39:44