I have a chat with private message but the private message doesnt work. This is the app.js
var express = require('express'),http = require('http');
var app = express();
var server = http.createServer(app);
console.log("Hola");
app.set('views',__dirname + '/views');
app.use(express.static(__dirname));
app.get('/',function(req,res){
res.render('index.jade',{layout:false});
});
app.post('/', function(req, res) {
if (req.session.logged)
{
res.send('Welcome back!');
console.log("Welcome back!");
}
else
{
req.session.logged = true;
res.send('Welcome!');
console.log("Welcome");
}
});
server.listen(1080);
function tstamp() {
var currentTime = new Date();
var days = new Array('Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab');
var day = currentTime.getDay();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
}
if (hours > 11) {
var ap = 'p';
}
else {
var ap = 'a';
}
if (hours > 12) {
hours = hours - 12;
}
return "["+ days[day] + " " + hours + ":" + minutes + ap + "m] ";
}
//websockets
var io = require('socket.io').listen(server);
var usuariosConectados = {};
var usuariosId ={};
io.sockets.on('connection',function(socket){
socket.on('enviarNombre',function(dato){
if(usuariosConectados[dato])
socket.emit('errorName');
else
{
// store the nickname in the socket session for this client
socket.nickname = dato;
//console.log("El usuario es: " + dato);
usuariosConectados[dato] = socket.nickname;
usuariosId[dato] = socket.id;
}
sId = [dato,usuariosId];
data = [dato,usuariosConectados];
io.sockets.emit('mensaje',data);
});
socket.on('enviarMensaje',function(mensaje){
var data = [socket.nickname, mensaje];
io.sockets.emit('newMessage',data);
});
socket.on('mensajePrivado',function(targetUser,msg) {
//console.log(sId[1][targetUser]);
//console.log(msg);
//console.log(socket.id);
console.log(tstamp());
io.sockets.socket(sId[1][targetUser]).emit('newPrivado', tstamp(), socket.nickname, targetUser, msg);
io.sockets.socket(socket.id).emit('newPrivadoSender', tstamp(), socket.nickname, targetUser, msg);
});
socket.on('disconnect',function(){
console.log("Entro a disconnect");
delete usuariosConectados[socket.nickname];
data = [usuariosConectados,socket.nickname];
io.sockets.emit('usuarioDesconectado',data);
});
});
The problem is when i use the listener "mensajePrivado" it never get to there and i dont know why. The socket.on('enviarMensaje') listener works fine.
I call it like this:
function sendPrivateMessage (targetUser)
{
console.log("entro en sendPrivateMessage");
var privateMsg = $('#'+ targetUser + 'privateMsg').val();
//verificamos que no tenga scripts
if((privateMsg.indexOf("<") != -1))
{
alert("Mensaje incorrecto");
}
else if((privateMsg.indexOf(">") != -1))
{
alert("Mensaje incorrecto");
}
else if((privateMsg.indexOf(";") != -1))
{
alert("Mensaje incorrecto");
}
else
{
$('#'+ targetUser + 'privateMsg').val("");
console.log(privateMsg);
console.log(targetUser);
websocket.emit('mensajePrivado', targetUser, privateMsg);
}
}
Also when i inspect elements in the Chrome browser i get the following error
failed: Error during WebSocket handshake: 'Connection' header value must contain 'Upgrade'
How can i fix all this?
Thanks in advance!
via user995691
No comments:
Post a Comment