Tiếp theo là các mã ở phía trước, nơi storeSelUserId
chứa user_id để gửi message-Gửi thông báo cập nhật cho người dùng đặc biệt là sử dụng socket.io
FYI - Node Version 1.1.0
// Socket Notification
var socket = io('http://localhost:6868');
socket.on('connection', function (data) {
socket.emit('send notification', { sent_to: storeSelUserId });
});
Sau đây là mã máy chủ trong các tệp tuyến đường -
var clients = {};
io.on('connection', function (socket) {
socket.emit('connection', "Connection Created.");
socket.on('send notification', function (sent_to) {
console.log(sent_to);
});
});
Trong bảng điều khiển sent_to
đang hiển thị mảng của user_id
.
Bây giờ, hãy bắt đầu bằng socket.io
Tôi đã mắc kẹt với giải pháp làm cách nào để gửi thư đến những người dùng cụ thể này.
tôi tìm kiếm và phát hiện ra rằng tôi cần phải đẩy mỗi người dùng với ổ cắm của nó vì vậy tôi cải nó -
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);
});
});
Nhưng tôi đang ở tình trạng khó xử đó khác tôi cần phải làm gì để ghi nhận những user_id
đề cập đến đó socket_id và sau đó cập nhật div của người dùng với id cụ thể đó?
EDIT -
Thêm điều khiển - (Front End)
cuối giao diện Mặt trận nơi ghi nhớ được tạo ra và gửi cho người dùng đặc biệt
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]});
});
khiển Dashboard - (Giao diện người dùng)
Giao diện Mặt trận cuối cùng mà đếm thông báo để hiển thị "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;
});
}
Mã ở cuối máy chủ -
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);
});
});
});
Backend mã điều khiển -
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++;
}
};
Hãy tìm edit..I của tôi không nhận được cập nhật tự động trên side..I khách hàng phải refresh lại trình duyệt sau đó chỉ cập nhật phản ánh – Trialcoder
Hãy cho tôi biết nếu có điều gì đó vẫn chưa rõ ràng – Trialcoder