Skip to content

Instantly share code, notes, and snippets.

@olore
Created July 26, 2011 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olore/1107648 to your computer and use it in GitHub Desktop.
Save olore/1107648 to your computer and use it in GitHub Desktop.
node client/server using socket.io
<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>
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