public
Created

  • Download Gist
index.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form id="chat">
<fieldset id="blabla">
</fieldset>
 
<fieldset>
<label>
nickname: <input type="text" name="nickname" value="anonymous" />
</label>
<label>
message: <input type="text" name="line" value="" />
</label>
<button type="submit">Send</button>
</fieldset>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
 
// message sending
$('#chat').submit(function (e) {
e.preventDefault();
 
var message = $('input[name="line"]')
, nickname = $('input[name="nickname"]');
 
if (!message.val().length) return;
 
socket.emit('chat', { nickname: nickname.val(), line: message.val() });
message.val('');
});
 
// chat messages
socket.on('chat', function (data) {
// escape html
function esc (html) { return new Option(html).innerHTML }
 
$('<p><strong>' + esc(data.nickname) + ':</strong> ' + esc(data.line) + '</p>')
.appendTo('#blabla');
})
</script>
</body>
</html>
server.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
var connect = require('connect')
, app = connect.createServer(connect.static(__dirname));
 
// require the new redis store
var RedisStore = require('./socket.io-master/lib/stores/redis')
, io = require('socket.io').listen(app);
 
// configure socket.io
io.set('transports', ['xhr-polling', 'jsonp-polling']);
io.set('store', new RedisStore);
 
// same shit different server
io.sockets.on('connection', function (socket) {
socket.on('chat', function (data) {
console.log('zing, ping pong boom');
socket.broadcast.emit('chat', data);
})
});
 
app.listen(process.env.CLUSTER_PORT || 8080);
console.log('Listening on', app.address())

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.