Skip to content

Instantly share code, notes, and snippets.

@TheRyanBurke
Created February 7, 2013 16:30
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 TheRyanBurke/4732158 to your computer and use it in GitHub Desktop.
Save TheRyanBurke/4732158 to your computer and use it in GitHub Desktop.
`npm install socket.io`, `npm install express`, `node app.js` to run
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<h1>1</h1>
<div id="content"></div>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('1a', function (data) {
console.log("received msg on '1a'");
$('div#content').html(data);
});
</script>
</body>
</html>
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(8080);
app.get('/1', function (req, res) {
res.sendfile(__dirname + '/1.html');
});
app.get('/2', function (req, res) {
res.sendfile(__dirname + '/2.html');
});
app.get('/3', function (req, res) {
res.sendfile(__dirname + '/3.html');
});
app.get('/4', function (req, res) {
res.sendfile(__dirname + '/4.html');
});
app.get('/control', function (req, res) {
res.sendfile(__dirname + '/control.html');
});
io.sockets.on('connection', function (socket) {
socket.on('1', function (data) {
console.log("RYAN: received msg on '1'");
socket.emit('1a', data);
});
});
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<h1>Control</h1>
<button id="update1">Update 1</button>
<button id="update2">Update 2</button>
<button id="update3">Update 3</button>
<button id="update4">Update 4</button>
<script>
var socket = io.connect('http://localhost:8080');
$('button#update1').click(function() {
console.log('clicked button 1');
socket.emit('1', { content: 'stuff' });
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment