Find file
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (61 sloc) 1.74 KB
<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 }
<div class="container">
<form id="chat">
<fieldset id="blabla" class="row">
<fieldset class="row actions">
<div class="clearfix">
<div class="input">
<input type="text" name="nickname" value="anonymous" />
<div class="clearfix">
<div class="input">
<input type="text" name="line" value="" />
<button type="submit" class="btn info">Send</button>
<script src="/"></script>
var socket = io.connect();
// message sending
$('#chat').submit(function (e) {
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);
// chat messages
socket.on('chat', function (data) {
// escape html
function esc (html) { return new Option(html).innerHTML }
$('<p><span class="label notice">'+ esc(data.nickname) +'</span>' + esc(data.line) + '</p>')