Last active
August 29, 2015 14:17
-
-
Save therealplato/c5786be5129f6854a73d to your computer and use it in GitHub Desktop.
pubnub federated game servers
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
// 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), | |
}); | |
}); |
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
// 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