Created
August 12, 2011 02:37
-
-
Save tonycosentini/1141327 to your computer and use it in GitHub Desktop.
NodeChat
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<script type="text/javascript" src="http://localhost:3000/socket.io/socket.io.js"></script> | |
<script src="./jquery-1.6.2.min.js"></script> | |
<script type="text/javascript"> | |
var socket = io.connect('http://localhost:3000/'); | |
var handle = ""; | |
while (handle == "") | |
handle = $.trim(prompt("Enter your chat handle:", "")); | |
socket.on('message', function (data) { | |
$("#messages").append("<tr><td class='handle'><strong>" + data.handle + "</strong></td><td>" + data.contents + "</td></tr>"); | |
$("html, body").animate({ scrollTop: $(document).height()-$(window).height() }, 'fast'); | |
}); | |
$(document).ready(function() { | |
$("#message").focus(); | |
$("#message").keyup(function (event) { | |
if (event.keyCode == 13) { // Return pressed | |
if ($("#message").val() != "") { | |
socket.emit('message', { handle: handle, contents: $("#message").val() }); | |
$("#message").val(''); | |
} | |
} | |
}); | |
}); | |
</script> | |
<style type="text/css"> | |
body { | |
margin: 0px; | |
padding: 0px; | |
font-family: "Helvetica Neue", Verdana, sans-serif; | |
} | |
input#message { | |
width: 100%; | |
position: fixed; | |
bottom: 0px; | |
font-size: 14pt; | |
height: 20px; | |
padding: 10px; | |
} | |
th { | |
position: fixed; | |
} | |
table { | |
margin-bottom: 50px; | |
padding: 10px; | |
} |
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 port = process.env.PORT || 3000; | |
var express = require('express'); | |
var app = express.createServer() | |
, io = require('socket.io').listen(app); | |
app.listen(port); | |
app.use("/", express.static(__dirname + '/static')); | |
io.sockets.on('connection', function (socket) { | |
console.log("connect!"); | |
socket.on('message', function (data) { | |
console.log("sending message!"); | |
io.sockets.emit('message', data); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment