Created
July 26, 2011 19:06
-
-
Save olore/1107648 to your computer and use it in GitHub Desktop.
node client/server using socket.io
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
<html> | |
<body> | |
<div id="status"></div> | |
<br /> <br /> <br /> | |
<div id="msg"></div> | |
</body> | |
<script src="http://localhost:4444/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect('http://localhost:4444'); | |
socket.on('registered', function(id) { | |
document.getElementById("status").innerHTML = "I got registered & my id is " + id; | |
}); | |
socket.on('cell_update', function (data, fn) { | |
msg = document.getElementById("msg"); | |
msg.innerHTML += "Got cell_update: "; | |
msg.innerHTML += "<br />" + JSON.stringify(data) + " <br />"; | |
msg.innerHTML += "Cell Label: <b>" +data.cells[0].label + "</b>"; | |
fn("got it"); | |
}); | |
</script> | |
</html> |
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 express = require('express'); | |
var app = express.createServer(); | |
var io = require('socket.io').listen(app); | |
app.listen(4444); | |
io.sockets.on('connection', function (socket) { | |
console.log('New connection'); | |
socket.emit('registered', 4000); | |
handle_new_user(socket); | |
}); | |
function handle_new_user(socket) { | |
//wait 5 sec then send down a cell update | |
setTimeout(function() { | |
var cell = {cells: [{color: 'red', label: 'This is a test'}]}; | |
socket.emit('cell_update', cell, function(data){ | |
console.log('client got the cell_update: ' + data); | |
}); | |
}, 5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment