Created
April 13, 2014 10:49
-
-
Save lpinca/10578821 to your computer and use it in GitHub Desktop.
Heroku Primus wss test
This file contains hidden or 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
(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' }); | |
})(); |
This file contains hidden or 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> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<p id="latency"></p> | |
<script src="/primus/primus.js"></script> | |
<script src="/client.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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": "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" | |
} | |
} |
This file contains hidden or 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
web: node server.js |
This file contains hidden or 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
'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