Created
October 16, 2015 09:08
-
-
Save lpinca/c1b92a9d4ebae5b34574 to your computer and use it in GitHub Desktop.
Metroplex GH-13
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
'use strict'; | |
var Socket = require('primus').createSocket({ transformer: 'faye' }) | |
, one = new Socket('http://localhost:3001') | |
, two; | |
one.on('data', function (data) { | |
console.log('"one" received message: '+ data); | |
}); | |
one.on('open', function () { | |
two = new Socket('http://localhost:3002'); | |
two.on('data', function (data) { | |
console.log('"two" received message: '+ data); | |
}); | |
}); |
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": "forward.sparks", | |
"version": "1.0.0", | |
"description": "", | |
"main": "server.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"faye-websocket": "^0.10.0", | |
"metroplex": "primus/metroplex", | |
"omega-supreme": "0.0.6", | |
"primus": "^4.0.0" | |
} | |
} |
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
'use strict'; | |
var Primus = require('primus') | |
, http = require('http'); | |
var sparks = []; | |
(function startServer(count) { | |
if (!count) return; | |
var server = http.createServer(); | |
var primus = new Primus(server, { | |
transformer: 'faye' | |
}); | |
primus.use('omega-supreme', 'omega-supreme'); | |
primus.use('metroplex', 'metroplex'); | |
primus.on('connection', function (spark) { | |
console.log(spark.id + ' connected'); | |
sparks.push(spark.id); | |
if (sparks.length !== 2) return; | |
primus.forward.sparks(sparks, 'hi', function (err, data) { | |
if (err) throw err; | |
console.log(data); | |
}); | |
}); | |
primus.on('disconnection', function (spark) { | |
console.log(spark.id + ' disconnected'); | |
sparks.splice(sparks.indexOf(spark.id), 1); | |
}); | |
server.listen(3000 + count, '0.0.0.0', function () { | |
var bound = server.address(); | |
console.log('server listening on '+ bound.address +':'+ bound.port); | |
}); | |
startServer(--count); | |
})(2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment