Created
October 2, 2014 15:03
-
-
Save peihsinsu/57575906a78ee84ba797 to your computer and use it in GitHub Desktop.
express4 compacted socket.io server and client
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
/** | |
* Socket server | |
* file: web.js | |
*/ | |
var app = require('express')(); | |
var server = require('http').Server(app); | |
var io = require('socket.io')(server); | |
server.listen(80); | |
app.get('/jquery.js', function (req, res) { | |
res.sendfile(__dirname + '/public/stylesheets/jquery.js'); | |
}); | |
app.get('/', function (req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
io.on('connection', function (socket) { | |
setInterval(function(){ | |
socket.emit('news', { ts: new Date().toString() }); | |
}, 3000); | |
socket.on('my other event', function (data) { | |
console.log(data); | |
}); | |
}); | |
/* | |
* Socket client | |
* file: index.html | |
<script src="/jquery.js"></script> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
$(function() { | |
var socket = io.connect('/'); | |
socket.on('news', function (data) { | |
console.log(data); | |
$('#ts').html(data.ts); | |
socket.emit('my other event', { my: 'data' }); | |
}); | |
}); | |
</script> | |
<h1 id='ts'></h1> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment