Hola, esto probando con un codigo de chat, preso resulta que cuando lo pruebo en loca, abriendo dos exploradores no puedo enviar mensajes entre usuarios. aprieto el boton "enviar mensaje" y la pagina no hace nada. aca el codigo
en el serivor:
var chatMultiApp = require('http').createServer(handleMultiChat);
var url = require('url');
var io = require('socket.io').listen(chatMultiApp);
chatMultiApp.listen(8124);
var connectedClients = [];
var userList = [];
function newMsg(socket, data) {
if(connectedClients.hasOwnProperty(socket.id)) {
var clientFrom = connectedClients[socket.id];
if(data.from == clientFrom.userName) {
if(data.type == 'PRIVATE') {
for(client in connectedClients) {
if(connectedClients[client].userName == data.to) {
connectedClients[client].socket.emit('msg', data);
}
}
} else {
socket.emit('msg', data);
socket.broadcast.emit('msg', data);
}
}
}
}
function handleMultiChat(request, response) {
var args = url.parse(request.url, true);
var queryString = args.query;
if(request.url == "/login") {
response.end("TODO... SOON... ");
}
if(queryString.hasOwnProperty("user")) {
if(queryString.user == "test" && queryString.pass == "test") {
response.writeHead(200);
var debugData = {};
debugData.users = connectedClients;
debugData.userNames = userList;
response.end(JSON.stringify(debugData));
}
} else {
response.writeHead(404);
response.end("404 NOT FOUND");
}
}
function assignUserName(userName, idSocket) {
connectedClients[idSocket].userName = userName;
}
function isUserNameAvailable(userName) {
for(var client in connectedClients) {
if(connectedClients[client].userName == userName) {
return false;
}
}
return true;
}
function makeUserList() {
userList = [];
var i = 0;
for(var client in connectedClients) {
userList[i] = {"userName" : connectedClients[client].userName};
i++;
}
}
io.sockets.on('connection', function(socket) {
connectedClients[socket.id] = {};
connectedClients[socket.id].socket = socket;
socket.on('authData', function(data) {
if(isUserNameAvailable(data.userName) == true) {
userList.push({"userName" : data.userName});
assignUserName(data.userName, socket.id);
socket.emit('userList', userList);
socket.broadcast.emit('userList', userList);
} else {
socket.emit('authData', {"isAvailable" : false});
}
});
socket.on('msg', function(data) {
newMsg(socket, data);
});
socket.on('disconnect', function(data) {
if(connectedClients.hasOwnProperty(socket.id)) {
delete connectedClients[socket.id];
}
makeUserList();
socket.broadcast.emit('userList', userList);
});
});
y el codigo en el cliente:
<html>
<head>
<title>:: CLIENTE DE CHAT MULTIUSUARIO ::</title>
<script src="http://127.0.0.1:8124/socket.io/socket.io.js" type="text/javascript" language="javascript"></script>
<script src="jquery.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
var socket = null;
var userName = null;
function selectUserName(userName) {
var selectUsers = document.getElementById("userList");
selectUsers.value = userName;
}
function hndlAuthData(data) {
if(data.isAvailable == false) {
alert("NOMBRE DE USUARIO YA COGIDO. ELIGE OTRO");
authenticate();
}
}
function authenticate() {
while(userName == null) {
userName = prompt('Inserta nombre de usuario');
}
socket.emit('authData', {"userName" : userName});
}
function refreshUserList(data) {
var selectUsers = document.getElementById("userList");
selectUsers.options.length = 0;
selectUsers.options[0] = new Option(" -- TODOS -- ", 0);
var html = "<table>";
for(var i = 0; i < data.length; i++) {
html += "<tr class='selectable'>";
html += "<td onClick='selectUserName(\""+data[i].userName+"\")'>" +data[i].userName+"</td>";
html += "</tr>";
selectUsers.options[(i+1)] = new Option(data[i].userName, data[i].userName);
}
html += "</table>";
$("#users").html(html);
}
function handleMessage(data) {
if(data.type == "PUBLIC") {
var htmlCurrent = $("#chat_screen").html();
htmlCurrent += "<b>" + data.from + " DICE: </b>" + data.message;
$("#chat_screen").html(htmlCurrent);
} else {
var htmlCurrent = $("#private_messages").html();
htmlCurrent += "<b>" + data.from + " DICE: </b>" + data.message;
$("#private_messages").html(htmlCurrent);
}
}
function sendMessage() {
var msg = {};
if($("#userList").val() != 0) {
msg.type = "PRIVATE";
msg.to = $("#userList").val();
} else {
msg.type = "PUBLIC";
}
msg.from = userName;
msg.message = $("#message").val();
socket.emit('msg', msg);
}
try {
socket = io.connect('http://127.0.0.1:8124');
socket.on('authData', hndlAuthData);
socket.on('userList', refreshUserList);
socket.on('msg', handleMessage);
} catch(e) {
alert(e);
}
</script>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body onLoad="authenticate();">
<div id="main_container">
<div id="users"></div>
<div id="chat_screen"></div>
<div id="message_entry">
<input type="text" name="message" id="message" /> para
<select name="userList" id="userList">
<option selected value="0"> -- TODOS -- </option>
</select>
<button id="sendMessage" name="sendMessage" onClick="sendMessage()">ENVIAR</button>
</div>
<div id="private_messages"></div>
</div>
</body>
</html>