Skip to content

Instantly share code, notes, and snippets.

@lpinca
Last active August 29, 2015 14:04
Show Gist options
  • Save lpinca/2dfe027205a55a860fa9 to your computer and use it in GitHub Desktop.
Save lpinca/2dfe027205a55a860fa9 to your computer and use it in GitHub Desktop.
`primus-rooms` GH-44
var primus = new Primus({ strategy: false });
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="/primus/primus.js"></script>
<script src="/client.js"></script>
</body>
</html>
{
"name": "gh-44",
"version": "0.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"primus": "^2.4.1",
"primus-rooms": "^3.0.5",
"ws": "^0.4.31"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "MIT"
}
'use strict';
var fs = require('fs')
, Primus = require('primus');
var 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);
});
var primus = new Primus(server, { transformer: 'websockets' });
primus.use('rooms', 'primus-rooms');
primus.on('connection', function (spark) {
spark.leaveAll(function () {
console.log('called when the spark has not joined any room');
spark.join('news sport', function () {
console.log(spark.rooms());
spark.leaveAll(function () {
console.log('called only once when the spark has joined multiple rooms');
});
});
});
});
server.listen(3000, function () {
console.log('listening on port 3000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment