Javascript Nodejs只有特定的登录用户才能获取消息
当用户在线并且不关闭我们的客户端(如浏览器选项卡或android应用程序)时,我可以通过以下方式向每个特定用户发送消息:Javascript Nodejs只有特定的登录用户才能获取消息,javascript,node.js,Javascript,Node.js,当用户在线并且不关闭我们的客户端(如浏览器选项卡或android应用程序)时,我可以通过以下方式向每个特定用户发送消息: socket.broadcast.to(socketId) .emit('new message', { username: data.fromUsername, money : 'Hurrraaa' }); 当用户将客户端作为移动应用程序关闭时,不会触发此事件,但我可以发送任何消息进行广播: socket.broadca
socket.broadcast.to(socketId)
.emit('new message', {
username: data.fromUsername,
money : 'Hurrraaa'
});
当用户将客户端作为移动应用程序关闭时,不会触发此事件,但我可以发送任何消息进行广播:
socket.broadcast.emit('new message', "hooooorrrrraaaaa");
我的用户在任何时候都不使用客户端应用程序,但我需要向特定用户发送消息并通知用户,直到打开应用程序并看到消息,用户应该在我的应用程序中准时获得我想从服务器发送的每一条消息,就像聊天信使一样,目前不需要用户使用WhatsApp等应用程序,我如何解决这个问题
然后,问题是当一些特定的用户安装了应用程序并记录了ti服务器时,向他们发送消息,但不使用now和应用程序等待接收消息,例如广播或特殊消息给他自己
此代码是我的简化服务器:
var socket = require('socket.io'),
express = require('express'),
app = express(),
server = require('http').createServer(app),
io = socket.listen(server),
port = process.env.PORT || 3000,
mysql = require('mysql'),
uuid = require('node-uuid'),
datetime = require('node-datetime'),
moment = require('moment'),
bcrypt = require('bcrypt'),
async = require('async'),
request = require('request'),
redis = require("redis"),
redisClient = redis.createClient(),
forever = require('forever'),
log = require('log4node');
var io_redis = require('socket.io-redis');
io.adapter(io_redis({host: 'localhost', port: 6379}));
require('sticky-socket-cluster/replace-console')();
var options = {
workers : require('os').cpus().length,
first_port : 8000,
proxy_port : 3000,
session_hash: function (req, res) {
return req.connection.remoteAddress;
},
no_sockets: false
};
require('sticky-socket-cluster')(options, start);
function start(port) {
io.sockets.on('connection', function (socket) {
socket.on('new message', function (data) {
socket.broadcast.emit('new message', "hooooorrrrraaaaa");
});
socket.on('login', function (data) {
log.info(JSON.stringify(data))
login(data.username, data.password, function (success, value) {
if (success) {
redisClient.exists(data.username, function (err, doesExist) {
if (err) return;
if (!doesExist) {
redisClient.set(data.username, socket.id, function (err, res) {
redisClient.set(data.username, socket.id);
});
}
else {
redisClient.del(data.username);
redisClient.set(data.username, socket.id, function (err, res) {
redisClient.set(data.username, socket.id);
});
}
});
socket.emit('login', {
result : true,
id : value.id,
registeredMobileNumber: value.registeredMobileNumber
});
} else {
socket.emit('login', {result: false});
}
});
});
socket.on('userConnected', function (username) {
redisClient.exists(username, function (err, doesExist) {
if (err) return;
if (!doesExist) {
redisClient.set(username, socket.id, function (err, res) {
redisClient.set(username, socket.id);
});
}
else {
redisClient.del(username);
redisClient.set(username, socket.id, function (err, res) {
redisClient.set(username, socket.id);
});
}
});
});
socket.on('disconnectUser', function (data) {
redisClient.exists(data.username, function (err, doesExist) {
if (err) return;
if (doesExist) {
redisClient.del(data.username);
}
});
});
server.listen(port, function () {
console.log('Express and socket.io listening on port ' + port);
});
}
您可以使用
socket.on('disconnect',function(){})代码>
- 当用户断开连接时,保存用户的用户id
- 关于用户id的后续消息将保存在服务器中
- 当客户端重新连接时,获取最新消息的时间,然后在该时间之后(保存在服务器中)将消息推送到客户端
此解决方案无法为用户实现准时功能,因为当用户断开连接时,则无法获取消息,但我希望用户在线,并在每次使用轮询时获取消息,当用户关闭应用程序时,保持后台服务运行,该服务将定期检查新邮件是否到达。您的意思是轮询而不是websocket
?将客户端传输设置为轮询?这可能是一种方法,但我所说的是定期连接并检查客户端中的新消息。大约每5秒钟连接一次服务器。没错,我似乎没有其他解决方案,我必须每5秒钟重新连接一次服务器