Skip to content

Instantly share code, notes, and snippets.

@therealplato
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therealplato/c5786be5129f6854a73d to your computer and use it in GitHub Desktop.
Save therealplato/c5786be5129f6854a73d to your computer and use it in GitHub Desktop.
pubnub federated game servers
// gameServer.js
// npm install --save pubnub
// http://www.pubnub.com/docs/javascript/javascript-sdk.html
var j$ = JSON.stringify;
var jP = JSON.parse;
var game = require('./game.js'); // Local game server
var queue = []; // Global matchmaking queue
var pubnub = require("pubnub")({
ssl: true, publish_key: "demo", subscribe_key: "demo"
});
pubnub.publish({
channel : "server.connected",
message : j$({
ip: process.env['IP'],
port: process.env['PORT'],
country: process.env['COUNTRY'],
})
});
pubnub.subscribe({
channel : "queue.update",
callback : function(q){
queue = jP(q);
game.updateState();
},
});
game.on('player.requestMatch', function(event){
pubnub.publish({
channel : "match.request",
message : j$(event.player),
});
});
// queueServer.js
var j$ = JSON.stringify;
var jP = JSON.parse;
var matchmaking = require('./match.js');
queue = [];
pubnub.subscribe(
channel: 'match.request',
callback: function(player){
var newQueue = matchmaking.placePlayer(queue, player);
queue = newQueue;
pubnub.publish({
channel:'queue.update',
message: j$(newQueue),
})
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment