Skip to content

Instantly share code, notes, and snippets.

@sintaxi
Created April 8, 2011 04:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sintaxi/909295 to your computer and use it in GitHub Desktop.
Save sintaxi/909295 to your computer and use it in GitHub Desktop.
a simple chat system
var q= function(s){ return document.getElementById(s) }
function send(){
var input = q('text');
socket.send(input.value);
input.value = '';
}
var socket = new io.Socket(null, {
port: 8001,
rememberTransport: false
});
socket.on('message', function(message){
var el = document.createElement('p');
el.innerHTML = message;
q('chat').appendChild(el);
});
socket.connect();
!!! 5
html(lang="en")
head
title chat
meta(charset="utf-8")
link(rel="stylesheet", href="/master.css")
script(src="http://cdn.socket.io/stable/socket.io.js")
script(src="/application.js")
body
h1 Sample chat client
div#chat
form#form(onsubmit="send();return false")
input#text(type="text", autocomplete="off")
#chat p {
padding: 8px;
margin: 0;
}
#chat p:nth-child(odd) {
background: #F6F6F6;
}
form {
background: #444;
padding: 5px 10px;
}
form input[type=text] {
width: 98%;
padding: 5px;
background: #fff;
}
{
"name": "simplechat",
"version": "0.0.2",
"dependencies": {
"coffee-script": "1.0.1",
"connect": "1.3.0",
"express": "2.2.1",
"jade": "0.10.4",
"socket.io": "0.6.17"
}
}
express = require "express"
socketio = require "socket.io"
app = express.createServer express.static(__dirname + '/public')
app.set('view engine', 'jade')
app.get "/", (req, rsp) ->
rsp.render 'index', { layout: false }
app.all "*", (req, rsp) ->
rsp.send 404
app.listen process.env.PORT || 8001
socket = socketio.listen(app)
socket.on "connection", (client) ->
client.send "welcome!"
client.broadcast "#{client.sessionId} connected"
client.on "message", (message) ->
client.send "you: #{message}"
client.broadcast "#{client.sessionId}: #{message}"
client.on "disconnect", ->
client.broadcast "#{client.sessionId} disconnected"
console.log "app listening on port #{ app.address().port }..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment