你假设我们有两个注册用户,分别是User1和User2,两者都登录到系统,现在User1想要向User2发送消息,发送消息给他后,我的nodejs服务器说User2没有连接,当User2想要发送消息给User1我得到这个通知哪个User1没有连接,但是他们两个用户都已连接,
现在
但他们无法向其他用户发送消息
var socket = require('socket.io'),
express = require('express'),
app = express(),
server = require('http').createServer(app),
io = socket.listen(server),
port = process.env.PORT || 3000,
redis = require("redis"),
redisClient = redis.createClient();
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('login', function (data) {
console.log(data.username);
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});
} else {
socket.emit('login', {result: false});
}
});
});
socket.on('requestMoney', function (data) {
redisClient.get(data.fromUsername, function (err, socketId) {
if (io.sockets.connected[socketId]) {
log.info('message to '+ data.fromUsername + ' sent');
io.sockets.connected[socketId].emit('message', {message: 'Hey !!'});
} else {
log.info(data.fromUsername + ' NOT Login');
}
});
});
});
server.listen(port, function () {
console.log('Express and socket.io listening on port ' + port);
});
}
答案 0 :(得分:1)
而不是
io.sockets.connected[socketId].emit(''));
我使用此代码:
socket.broadcast.to(socketId).emit(''));
现在我可以向特定用户发送消息,从web到Android登录用户,从android设备发送到Web登录用户
我的Html测试代码给任何想要了解更多内容的人
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>
Document
</title>
</meta>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#login').click(function () {
socket.emit('login', {username: 'mahdi', password: 'a'});
});
$('#mesage').click(function () {
socket.emit('requestMoney', {fromUsername: 'a'});
});
});
</script>
<script>
var socket = new io.connect('http://192.168.1.35:3000', {
transports: ['websocket']
});
socket.on('connect', function () {
console.log('connected!');
});
socket.on('message', function (message) {
console.log("hello ::::::::" + message);
});
socket.on('success', function (data) {
console.log(data);
});
</script>
</head>
<body>
<h3 id="login">login</h3>
<h3 id="mesage">mesage</h3>
</body>
</html>