Last active
August 29, 2015 14:04
-
-
Save lpinca/2dfe027205a55a860fa9 to your computer and use it in GitHub Desktop.
`primus-rooms` GH-44
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
var primus = new Primus({ strategy: false }); |
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> | |
<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": "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" | |
} |
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') | |
, 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