-
-
Save dshaw/1233318 to your computer and use it in GitHub Desktop.
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> | |
<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> |
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
{ | |
"name": "3rdedentest" | |
, "version": "0.0.1" | |
, "dependencies": { | |
"connect" : "" | |
, "socket.io" : "https://github.com/LearnBoost/socket.io/tarball/master" | |
} | |
, "main": "./server.js" | |
, "engines": { "node": "~0.4.0" } | |
} |
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 connect = require('connect') | |
, app = connect.createServer(connect.static(__dirname)); | |
// require the new redis store | |
var sio = require('socket.io') | |
, RedisStore = sio.RedisStore | |
, io = sio.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); | |
socket.emit('chat', data); | |
}) | |
}); | |
app.listen(process.argv[2] || 8080); | |
console.log('Listening on', app.address()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment