Created
April 8, 2011 04:32
-
-
Save sintaxi/909295 to your computer and use it in GitHub Desktop.
a simple chat system
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 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(); |
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
!!! 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") |
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
#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; | |
} |
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": "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" | |
} | |
} |
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
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