gpt4 book ai didi

javascript - Nodejs只能向所有者消息发送者发送消息

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:13 26 4
gpt4 key购买 nike

假设我们有两个注册用户,即User1和User2,并且他们都登录到系统,现在User1想要向User2发送消息,向他发送消息后,我的nodejs服务器说User2未连接,当User2想要向User1发送消息时,我收到此通知,User1未连接,但他们两个用户都已连接,

现在

  • 用户1只能向自己发送消息
  • 用户2只能向自己发送消息

但他们无法向其他用户发送消息

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);
});
}

最佳答案

问题已解决

而不是

io.sockets.connected[socketId].emit(''));

我使用这个代码:

socket.broadcast.to(socketId).emit(''));

现在我可以将消息从网络发送到 Android 登录用户以及从 Android 设备发送到网络登录用户

我的 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>

关于javascript - Nodejs只能向所有者消息发送者发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38853744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com