Created
March 12, 2014 16:32
-
-
Save cayasso/9510749 to your computer and use it in GitHub Desktop.
Primus Quick 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
<!Doctype html> | |
<html> | |
<head> | |
<script src="/primus/primus.js"></script> | |
</head> | |
<body> | |
<script> | |
var primus = new Primus('ws://localhost:5000', { transformer: 'engine.io' }); | |
primus.on('open', function () { | |
console.log('OPEN'); | |
}); | |
primus.on('reconnect', function () { | |
console.log('RECONNECT'); | |
}); | |
primus.on('reconnecting', function () { | |
console.log('RECONNECTING'); | |
}); | |
</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
{ | |
"name": "example", | |
"version": "0.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"primus": "2.0.2", | |
"engine.io": "~1.0.1" | |
} | |
} |
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
var Primus = require('primus') | |
, http = require('http') | |
, fs = require('fs'); | |
var server = http.createServer(function server(req, res) { | |
res.setHeader('Content-Type', 'text/html'); | |
fs.createReadStream(__dirname + '/index.html').pipe(res); | |
}); | |
var primus = new Primus(server, { transformer: 'engine.io' }); | |
primus.on('connection', function (spark) { | |
console.log('CONNECTING ===>', spark.id); | |
}); | |
// Start server listening | |
server.listen(process.env.PORT || 5000, function(){ | |
console.log('\033[96mlistening on localhost:5000 \033[39m'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment