Last active
May 26, 2016 04:16
-
-
Save jcarroyo/5f4e3fcc8cb25585fc59 to your computer and use it in GitHub Desktop.
Chat server and client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var readline = require('readline'); | |
var net = require('net'); | |
var port = process.env.CHATPORT || 8090; | |
var client = new net.Socket(); | |
client.connect(port, '127.0.0.1', function() { | |
console.log('Connected'); | |
}); | |
client.setEncoding("utf8"); | |
client.on('data', function(data) { | |
console.log("Data recibida", data); | |
rl.prompt(); | |
}); | |
client.on('close', function() { | |
console.log("conexión cerrada") | |
}); | |
client.on('connect', function(){ | |
console.log("conectado!"); | |
setTimeout(function(){ | |
rl.prompt(); | |
}, 2000); | |
}) | |
client.on('error', function(error){ | |
console.log("error", error) | |
}) | |
var rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
rl.setPrompt("comando>"); | |
rl.prompt(); | |
rl.on('line', function(line){ | |
if(line == "salir"){ | |
process.exit(); | |
} | |
client.write(line); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var util = require('util'); | |
var commands = ['nick:', 'broadcast:', 'chat:', 'list:']; | |
var clients = []; | |
var port = process.env.CHATPORT || 8090; | |
var server = net.createServer(function(socket){ | |
socket.setEncoding("utf8"); | |
socket.name = socket.remoteAddress + ":" + socket.remotePort; | |
clients.push(socket); | |
broadcast("cliente " + socket.name + " se conectó"); | |
socket.on('close', function(){ | |
var message = "usuario se desconectó"; | |
clients.splice(clients.indexOf(socket), 1); | |
broadcast(message); | |
}) | |
socket.on('data', function(data){ | |
var valid = false; | |
commands.forEach(function(command){ | |
if(~data.indexOf(command)){ | |
socket.emit(command, data.substr(command.length, data.length)); | |
valid = true; | |
} | |
}); | |
if(!valid){ | |
console.log("comando no reconocido!", data); | |
socket.emit('messageError', "comando no reconocido"); | |
} | |
}) | |
socket.on('error', function(error){ | |
console.log("error con socket cliente!!!", error) | |
}) | |
//custom events | |
socket.on('nick:', function(newNick){ | |
var oldNick = socket.name; | |
socket.name = newNick; | |
var message = util.format("changeNick:%s:%s", oldNick, newNick); | |
socket.write(message); | |
}); | |
socket.on('list:', function(){ | |
var list = [] | |
clients.forEach(function(cli){ | |
list.push(cli.name); | |
}) | |
var message = util.format("list:%s", list.join(',')); | |
socket.write(message); | |
}) | |
socket.on('broadcast:', function(message){ | |
var message = util.format("broadcast:%s:%s", socket.name, message) | |
broadcast(message); | |
}) | |
//message: name:message | |
socket.on('chat:', function(message){ | |
var parts = message.split(":"); | |
if(parts.length != 2){ | |
return socket.on('messageError', 'formato inválido en el protocolo para chat directo'); | |
} | |
var to = parts[0], text = parts[1]; | |
var message = util.format("chatFrom:%s:%s", socket.name, text); | |
sendDirectMessage(to, message); | |
}) | |
socket.on('messageError', function(message){ | |
var message = util.format("error:%s", message); | |
socket.write(message); | |
}) | |
function broadcast(text){ | |
clients.forEach(function(c){ | |
c.write(text); | |
}) | |
} | |
function sendDirectMessage(clientName, message){ | |
var sended = false; | |
clients.forEach(function(client){ | |
if(client.name == clientName){ | |
client.write(message); | |
sended = true; | |
} | |
}) | |
if(!sended){ | |
socket.emit('messageError', "no se pudo enviar mensaje a " + clientName); | |
} | |
} | |
}); | |
server.on('close', function(){ | |
console.log("bye!") | |
}) | |
server.on('error', function(error){ | |
console.log("error servidor!!!", error) | |
}) | |
server.on('connection', function(socket){ | |
console.log("connection") | |
}) | |
server.on('listening', function(){ | |
console.log("servidor escuchando en puerto " + port) | |
}) | |
server.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment