以下特定用戶是在前端的代碼,其中包含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++;
}
};
請找我edit..I我不是在客戶端side..I獲得自動更新已經刷新瀏覽器則只更新反映 – Trialcoder 2014-10-13 12:15:47
讓我知道,如果有什麼東西還不清楚 – Trialcoder 2014-10-13 12:19:01