Created
May 27, 2011 23:20
-
-
Save slickplaid/996384 to your computer and use it in GitHub Desktop.
Simple example
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
<!-- put me in public/index.html --> | |
<!doctype html> | |
<html> | |
<head><title>Testing Express, Socket.io in Node.js</title></head> | |
<body> | |
<h1>Test!</h1> | |
<div class="m"></div> | |
<button>Send Message</button> | |
<!-- CHANGE to server hostname:port/socket.io/socket.io.js --> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> | |
<script src="//hercules:3020/socket.io/socket.io.js"></script> | |
<script> | |
var socket = new io.Socket(); | |
socket.connect(); | |
socket.on('connect', function(){ $('.m').append('Connected at '+new Date()+'<br>'); }) | |
socket.on('message', function(message){ | |
if(message.alert){ $('.m').append('New Message: '+new Date()+': '+message.alert+'<br>'); } | |
console.log(message); | |
}) | |
socket.on('disconnect', function(){ $('.m').append('Disconnected at '+new Date()+'<br>'); }) | |
$(function(){ | |
$('button').click(function(){ | |
$('.m').append('Sending Message: '+new Date()+': "Sent at '+new Date()+'"<br>'); | |
socket.send({ alert: 'Sent at '+new Date() }); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
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
connect = require 'connect' | |
express = require 'express' | |
io = require 'socket.io' | |
app = express.createServer(); | |
app.configure -> | |
app.set 'views', __dirname + '/views' | |
app.use connect.bodyParser() | |
app.use connect.static __dirname + '/public' | |
app.use app.router | |
app.listen 3020 | |
io = io.listen app | |
io.on 'connection', (client)-> | |
client.broadcast JSON.stringify { alert: "User connected! Server time is now #{ new Date() }" } | |
client.on 'message', (message) -> | |
client.broadcast message | |
client.send message | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That still doesn't work you know. App isn't defined before require express.