Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // evaluating libs with vars in current context instead of 'require'
- var util = require('util')
- var vm = require("vm");
- var fs = require("fs");
- var load = function(path, context) {
- var data = fs.readFileSync(path);
- vm.runInThisContext(data, path);
- }
- var WebSocketClient = require("websocket").client
- // adapter for browser websocket
- WebSocket = function (uri) {
- console.info("============", uri);
- var self = this;
- this.connection = null;
- this.socket = new WebSocketClient();
- this.socket.on('connectFailed', function (errorDescription) {
- self.onerror(errorDescription);
- })
- this.socket.on('connect', function (connection) {
- console.info("============");
- self.connection = connection;
- connection.on('error', function (error) {
- console.info("WS error:", error);
- self.onerror(error);
- });
- connection.on('close', function (ev) {
- console.info("WS close:", ev);
- self.onclose(ev);
- });
- connection.on('message', function (message) {
- // console.info("WS message:", message);
- if (message.type === 'utf8') {
- self.onmessage({data:message.utf8Data});
- }
- return true;
- });
- self.onopen();
- });
- this.socket.connect(uri);
- }
- WebSocket.prototype.send = function (data) {
- if(this.connection)
- this.connection.sendUTF(data);
- }
- WebSocket.prototype.close = function () {
- if(this.connection)
- this.connection.close();
- }
- ///////
- load("./photon/Photon-Javascript_SDK.js")
- var LBC = Photon.LoadBalancing.LoadBalancingClient;
- var lbc = new LBC(Photon.ConnectionProtocol.Ws, process.env.PHOTONAPPID, "0.3.1a");
- lbc.regions = null;
- lbc.connectToNextRegion = false;
- lbc.setLogLevel(Exitgames.Common.Logger.Level.WARN);
- lbc.onStateChange = function (state)
- {
- console.info("State:", LBC.StateToName(state));
- switch (state) {
- case LBC.State.ConnectedToNameServer:
- if (lbc.regions == null) {
- lbc.getRegions();
- }
- break;
- case LBC.State.ConnectedToMaster:
- break;
- case LBC.State.Disconnected:
- if (lbc.connectToNextRegion)
- {
- lbc.connectToNextRegion = false;
- lbc.connect();
- }
- break;
- default:
- break;
- }
- };
- lbc.onEvent = function (code, data) {
- console.info("Event:", code, data);
- lbc.raiseEvent(code, data);
- }
- lbc.onOperationResponse = function (errorCode, errorMsg, code, content) {
- //console.info("op resp:", errorCode, errorMsg, code, content);
- if (errorCode) {
- switch (code) {
- default:
- console.error("Operation Response error:", errorCode, errorMsg, code, content);
- break;
- }
- }
- };
- lbc.onGetRegionsResult = function (errorCode, errorMsg, regions)
- {
- if (errorCode != 0) {
- console.error("onGetRegionsResult:", errorCode, errorMsg);
- return;
- }
- lbc.regions = [];
- lbc.regionIndex = 0;
- console.info("Photon regions: ");
- // create array of IP addresses
- for (var key in regions) {
- if (regions.hasOwnProperty(key))
- {
- var region = {
- code: key,
- address: regions[key],
- masterPeerCount: 0,
- peerCount: 0,
- gameCount: 0
- }
- lbc.regions.push(region);
- console.info(key + " : " + regions[key]);
- }
- }
- if (lbc.regions.length > 0)
- {
- lbc.setMasterServerAddress(lbc.regions[0].address);
- lbc.connect();
- }
- };
- lbc.onLobbyStats = function (errorCode, errorMsg, lobbies)
- {
- if (errorCode != 0) {
- console.error("onLobbyStats:", errorCode, errorMsg);
- return;
- }
- console.info("onLobbyStats: " + lobbies.length);
- for (var i=0; i < lobbies.length; i++)
- {
- var lobby = lobbies[i];
- console.info("lobby: " + lobby.lobbyName + " lobbyType: " + lobby.lobbyType + " peerCount: " + lobby.peerCount + " gameCount: " + lobby.gameCount);
- }
- lbc.disconnect();
- };
- lbc.onAppStats = function (errorCode, errorMsg, stats)
- {
- if (errorCode != 0) {
- console.error("onAppStats:", errorCode, errorMsg);
- return;
- }
- console.info("appStats: " + stats.masterPeerCount + " " + stats.peerCount + " " + stats.gameCount);
- lbc.requestLobbyStats();
- if (lbc.regionIndex < lbc.regions.length)
- {
- var region = lbc.regions[lbc.regionIndex];
- console.info(region.code + " (" + (lbc.regionIndex+1) + "/" + lbc.regions.length + ") : games " + stats.gameCount + " master peers: " + stats.masterPeerCount + " peers " + stats.peerCount );
- region.masterPeerCount = Math.max(0, stats.masterPeerCount - 1); // - this bot
- region.peerCount = stats.peerCount;
- region.gameCount = stats.gameCount;
- lbc.regions[lbc.regionIndex] = region;
- // connect to next region
- lbc.regionIndex++;
- if (lbc.regionIndex < lbc.regions.length)
- {
- console.info("Connecting to: " + lbc.regions[lbc.regionIndex].code);
- lbc.setMasterServerAddress(lbc.regions[lbc.regionIndex].address);
- lbc.connectToNextRegion = true;
- }
- else
- {
- // regions done save to file
- fs.writeFile("./regions.json", JSON.stringify(lbc.regions, null, 2) , 'utf-8', function(err) {
- if(err) {
- return console.log(err);
- }
- console.log("Saved regions to file.");
- });
- }
- }
- };
- module.exports.lbc = lbc;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement