Created
December 5, 2011 14:17
-
-
Save joseprando/1433712 to your computer and use it in GitHub Desktop.
nodejs socket.io chat
This file contains hidden or 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
<!doctype html> | |
<html lang="pt-br"> | |
<head> | |
<title>socket.io chat</title> | |
<meta charset='utf-8'> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
function onLoad() { | |
var socket = io.connect('http://192.168.1.151:8000'); | |
var logBt = document.getElementById('loginButton'); | |
logBt.onclick = function(e) { | |
var nameInp = document.getElementById('nameInput'); | |
socket.emit('login', nameInp.value); | |
}; | |
var sendBt = document.getElementById('sendButton'); | |
sendBt.onclick = function(e) { | |
var msgTx = document.getElementById('mensagemInput'); | |
socket.emit('mensagem', msgTx.value + '\n'); | |
}; | |
socket.on('ready', function (data) { | |
var chatTx = document.getElementById('chatTextArea'); | |
chatTx.value += data + ' entrou\n'; | |
}); | |
socket.on('mensagemclient', function (msg, name) { | |
var chatTx = document.getElementById('chatTextArea'); | |
chatTx.value += name + ': ' + msg; | |
}); | |
} | |
window.onload = onLoad; | |
</script> | |
</head> | |
<body> | |
<h1>socket.io chat</h1> | |
<input id="nameInput" type="text"/> | |
<button id="loginButton">Login</button> | |
<br/> | |
<textarea id="chatTextArea" rows="5" cols="50"></textarea> | |
<br/> | |
<input id="mensagemInput" type="text"/> | |
<button id="sendButton">Send</button> | |
</body> | |
</html> |
This file contains hidden or 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 app = require('http').createServer(handler) | |
, io = require('socket.io').listen(app) | |
, fs = require('fs'); | |
function handler (req, res) { | |
req.setEncoding('utf-8'); | |
fs.readFile(__dirname + '/index.html', 'utf8', function (err, data) { | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading index.html'); | |
} | |
res.writeHead(200); | |
res.end(data); | |
}); | |
} | |
io.sockets.on('connection', function (socket) { | |
socket.on('login', function (name) { | |
socket.set('nickname', name, function() { | |
console.log(name); | |
socket.emit('ready', name); | |
socket.broadcast.emit('ready', name); | |
}); | |
}); | |
socket.on('mensagem', function (msg) { | |
socket.get('nickname', function (err, name) { | |
socket.emit('mensagemclient', msg, name); | |
socket.broadcast.emit('mensagemclient', msg, name); | |
}); | |
}); | |
socket.on('disconnect', function () { | |
socket.get('nickname', function (err, name) { | |
socket.broadcast.emit('mensagemclient', 'saiu', name); | |
}); | |
console.log('saiu fora'); | |
}); | |
}); | |
app.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment