我是node.js的新手。 如何使用socket.io从node.js服务器向特定用户发送通知。
我计划创建应用,用户可以将其他用户添加为他/她的朋友,以便他们成为朋友后可以互相聊天。
因此,一旦用户A向用户B发送了请求,用户B将获得有关该请求的实时通知。现在我有用户管理系统,包括登录和注册。在数据库中,我有两个表:包含所有用户注册详细信息的用户和包含用户朋友关系数据的朋友(friend_id_one,friend_id_two是引用users.user_id的FOREIGN KEY),状态和时间戳。所以,我插入声明添加这样的朋友:
INSERT INTO friends
(friend_id_one,friend_id_two)
VALUES
(user_id_one,friend_id_two);
我尝试向用户发送通知。
客户端:
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
form input { border: 0; padding: 10px; width: 10%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
</style>
</head>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button onclick="notifyMe()">Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('send notification', $('#m').val());
$('#m').val('');
return false;
});
socket.on('send notification', function(msg){
notifyMe(msg);
});
});
function notifyMe(msg) {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check if the user is okay to get some notification
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var options = {
body: msg,
dir : "ltr"
};
var notification = new Notification(msg,options);
}
// Otherwise, we need to ask the user for permission
// Note, Chrome does not implement the permission static property
// So we have to check for NOT 'denied' instead of 'default'
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// Whatever the user answers, we make sure we store the information
if (!('permission' in Notification)) {
Notification.permission = permission;
}
// If the user is okay, let's create a notification
if (permission === "granted") {
var options = {
body: msg,
dir : "ltr"
};
var notification = new Notification(msg,options);
}
});
}
// At last, if the user already denied any notification, and you
// want to be respectful there is no need to bother them any more.
}
</script>
</body>
</html>
服务器:
let usersMap = new Map;
io.on('connection', function(socket){
console.info(`Client connected socketId = ${socket.id}`);
console.log("session_id: " + socket.request.session.session_id);
usersMap.set(socket.request.session.session_id, socket.id);
socket.on('send notification', data => {
if(usersMap.has(parseInt(data))) {
io.sockets.connected[usersMap.get(parseInt(data))].emit('send notification', "Friend want add you to friends");
let today = helper.getDateNow();
let sql = "INSERT INTO `friends` (`friends_one`, `friends_two`, `created`) VALUES ('" + socket.request.session.session_id+"', '"+parseInt(data)+"', '"+ today+"') ";
connection.query(sql, (error, result ) => {
if(error){
console.log(error);
}
console.log("successfull");
});
} else {
console.log(`User is not found`);
}
});
socket.on('disconnect', function(){
usersMap.delete(socket.request.session.session_id);
console.log('user disconnected');
});
});
但是通知会获得所有用户。请帮我修复这个错误。