Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (61 sloc) 1.74 KB
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<script src="jquery.js"></script>
<link href="main.css" type="text/css" rel="stylesheet" />
<style type="text/css">
.btn { margin-left:310px }
#blabla p { border-bottom:1px solid #ccc; padding:5px 0 10px }
#blabla .label { margin-right:10px }
</style>
</head>
<body>
<div class="container">
<form id="chat">
<fieldset id="blabla" class="row">
</fieldset>
<fieldset class="row actions">
<div class="clearfix">
<label>nickname</label>
<div class="input">
<input type="text" name="nickname" value="anonymous" />
</div>
</div>
<div class="clearfix">
<label>message</label>
<div class="input">
<input type="text" name="line" value="" />
</div>
</div>
<button type="submit" class="btn info">Send</button>
</fieldset>
</form>
</div>
<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"]')
, data = { nickname: nickname.val(), line: message.val() };
if (!message.val().length) return;
socket.emit('chat', data);
// omg evil hidden internal function abuse!, just to prevent network overhead
// no need to broadcast to our selfs
socket.$emit('chat', data);
message.val('');
});
// chat messages
socket.on('chat', function (data) {
console.log(data);
// escape html
function esc (html) { return new Option(html).innerHTML }
$('<p><span class="label notice">'+ esc(data.nickname) +'</span>' + esc(data.line) + '</p>')
.appendTo('#blabla');
})
</script>
</body>
</html>