Created
April 20, 2011 18:58
-
-
Save sreeix/932330 to your computer and use it in GitHub Desktop.
Node JS. chat client.
This file contains 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 http = require('http'), | |
express = require("express"), | |
sys = require("sys"), | |
io = require('socket.io'); | |
var app = express.createServer(); | |
app.set('view engine', 'jade'); | |
app.get("/", function(req, res){ | |
res.render("index", {title: "Chatty", layout: false}); | |
}); | |
app.listen(9999); | |
// socket.io | |
var socket = io.listen(app); | |
socket.on('connection', function(client){ | |
client.on('message', function(data){ | |
sys.puts(data); | |
socket.broadcast(data); | |
}); | |
client.on('disconnect', function(){ | |
sys.puts("client diconnected"); | |
}); | |
}); |
This file contains 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= title | |
script(src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js') | |
script(src = 'http://localhost:9999/socket.io/socket.io.js') | |
script(type='text/javascript') | |
$( function(){ | |
$('button').click(function(){ | |
socket.send($('.text').val()); | |
return false; | |
}); | |
var socket = new io.Socket('localhost'); | |
socket.connect(); | |
socket.on('connect', function(){ | |
$(".messages").append("<h3>connected to localhost</h3>"); | |
}); | |
socket.on('message', function(data){ | |
$(".messages").append("<h2>"+data+"</h2>"); | |
}); | |
socket.on('disconnect', function(){ | |
$(".messages").append("<h3>disconnected</h3>"); | |
}); | |
}); | |
body | |
h1 Welcome to the chat server | |
#container | |
label Messages: | |
.messages(name='messages') | |
label Text: | |
input.text(name='Text') | |
button.send Send |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment