Skip to content

Instantly share code, notes, and snippets.

@lpinca
Created April 13, 2014 10:49
Show Gist options
  • Save lpinca/10578821 to your computer and use it in GitHub Desktop.
Save lpinca/10578821 to your computer and use it in GitHub Desktop.
Heroku Primus wss test
(function () {
var latency = document.getElementById('latency')
, primus = new Primus();
primus.on('open', function () {
latency.innerHTML = 'Connection established after ' + primus.latency + ' ms';
}).on('data', function (data) {
if (data.echo) console.log(data);
});
primus.write({ echo: true, message: 'hello' });
})();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p id="latency"></p>
<script src="/primus/primus.js"></script>
<script src="/client.js"></script>
</body>
</html>
{
"name": "sample",
"version": "0.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"primus": "git://github.com/primus/primus#394eeaf2c88f90b090545468def7b37f3a8abb18",
"ws": "^0.4.31"
},
"engines": {
"node": "0.10.x"
}
}
web: node server.js
'use strict';
var fs = require('fs')
, server
, port = process.env.PORT || 3000
, Primus = require('primus')
, primus;
server = require('http').createServer(function (req, res) {
if (req.url === '/client.js') {
res.setHeader('Content-Type', 'application/javascript');
fs.createReadStream(__dirname + '/client.js').pipe(res);
return;
}
res.setHeader('Content-Type', 'text/html');
fs.createReadStream(__dirname + '/index.html').pipe(res);
});
primus = new Primus(server);
primus.on('connection', function(spark) {
spark.on('data', function (data) {
if (data.echo) spark.write(data);
});
});
server.listen(port, function () {
console.log('listening on port ' + port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment