【发布时间】:2020-01-24 21:11:25
【问题描述】:
我的问题如下: 我有 2 个通过套接字服务器进行通信的应用程序。 App1 通过 message 事件向 app 2 发送消息。这可以正常工作,因为应用程序 2 获得了正确的消息,我正在尝试做的是从应用程序 2 向 1 发送另一条回复消息,以警告消息已顺利到达。为此,我有一个名为 estado_mensaje 的事件,该事件在消息事件中发出,并且在服务器上在侦听此事件时发出 mensaje_ok 事件,这在服务器控制台中显示良好,但我无法在应用程序 1 中收到该消息. 这是我的代码:
服务器节点
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 4444;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
UserOnId=new Array();
IdsOnUser=new Array();
io.on('connection', function(socket){
socket.on('datos_usuario', function(datos){
id_socket=socket.id;
usuario=datos.username;
UserOnId[id_socket]=usuario;
if(IdsOnUser[usuario]==null){
IdsOnUser[usuario]=new Array();
}
IdsOnUser[usuario].push(id_socket);
console.log('------usuarios por id_socket------');
console.log(UserOnId);
console.log('------IDs socket por usuarios------');
console.log(IdsOnUser);
console.log('Cantidad de usuarios en lineas');
console.log(Object.keys(IdsOnUser).length);
io.emit('nuevo_usuario', datos);
});
socket.on('message', function(datos){
if(datos.user_to!=''){
destinatario=datos.user_to;
if(IdsOnUser[destinatario]){
id_onlines=IdsOnUser[destinatario];
for (var i=0;i<id_onlines.length;i++){
io.to(id_onlines[i]).emit('message', datos);
}
io.to(socket.id).emit('message', datos);
}
}else {
io.emit('message', datos);
}
console.log(datos.contenido);
});
socket.on('estado_mensaje ', function(datos){
console.log(datos);
io.emit('ok_mensaje', datos);
});
App1
var socket = io();
$(function () {
$('form').submit(function(){
var id_usuario_logueado=$('#id_username').val();
var usuario_logueado=$('#username').val();
var id_user_to=$('#id_receptor').val();
var user_to=$('#receptor').val();
datos={id_user_from:id_usuario_logueado,user_from:usuario_logueado,id_user_to:id_user_to,user_to:user_to,contenido:$('#m').val()};
/*Envia un mensaje a traves del canal message*/
socket.emit('message', datos);
$('#m').val('');
return false;
});
socket.on('message', function(datos){
$('#messages').append($('<li>').text(datos.user_from+':'+datos.contenido));
console.log(datos);
window.scrollTo(0, document.body.scrollHeight);
});
socket.on('nuevo_usuario', function(datos){
console.log('Usuario logueado: '+datos.username);
var usuario_logueado=$('#usuario_logueado').val(datos.username);
});
socket.on('ok_mensaje ', function(datos){
alert('llega la respuesta');
console.log(datos);
});
});
function loguear(){
var username=$('#username').val();
socket.emit('datos_usuario', {username:username});
}
App2
socket.on('message', function(datos){
$.ajax({
url: "index.php?route=administracion/chat/send_message&token=" + token,
type: 'POST',
dataType: 'json',
async:true,
data:{ id_user_from : id_user_from,id_user_to: id_user_to },
success: function (json) {
/*Here is how I emit the response to the other app*/
socket.emit('estado_mensaje ', {status:'sucess'});
console.log('guarda bien');
},
error: function(json){
console.log('guarda mal');
socket.emit('estado_mensaje ', {status:'error'});
}
});
});
【问题讨论】:
-
如果 app1 是浏览器中的一个页面,您是否 100% 确定 app1 仍位于之前的同一页面上?请记住,当页面重新加载时,所有 socket.io 状态都会被清除(并且必须重新建立)。如果您从导致页面重新加载的表单提交中从 app1 发送,您可能会失去您的听众。我想请您向我们展示更多来自 app1 的代码上下文,以便我们可以准确了解不同部分的运行和触发方式。
-
你能告诉我们你在哪里
require你的socket.io linrary吗? -
我更新了上面的所有代码。这两个应用程序都没有重新加载页面。应用程序 1 是一个简单的表单,其中放置消息和将向其发送消息的用户,提交页面时不会重新加载。顺利到达,app2 发送带有确认消息的事件,在节点服务器控制台中显示良好,但在 app1 控制台中没有显示
标签: javascript node.js socket.io