diff --git a/app/beamQuest/main.js b/app/beamQuest/main.js index 9b1ec25..ff84695 100644 --- a/app/beamQuest/main.js +++ b/app/beamQuest/main.js @@ -13,6 +13,7 @@ var SessionStore = { } }; +var ping = require('beamQuest/ping'); exports.start = function(io) { var client = SessionStore; @@ -42,6 +43,8 @@ exports.start = function(io) { io.sockets.emit('notify:message', { message: data.message }); }); + ping.listen(socket); + socket.emit('connected'); }); }; diff --git a/app/beamQuest/ping.js b/app/beamQuest/ping.js new file mode 100644 index 0000000..f750699 --- /dev/null +++ b/app/beamQuest/ping.js @@ -0,0 +1,6 @@ +exports.listen = function(socket) { + socket.on('ping:update', function(data) { + socket.emit('notify:ping', data); + }); +}; + diff --git a/public/cocos2d.js b/public/cocos2d.js index 68538a3..d35f1d0 100644 --- a/public/cocos2d.js +++ b/public/cocos2d.js @@ -25,6 +25,7 @@ 'js/src/inputHandler.js', 'js/src/entity/entity.js', 'js/src/entity/player.js', + 'js/src/ping.js', 'js/src/beamQuest.js'//add your own files in order here ] }; diff --git a/public/js/src/beamQuest.js b/public/js/src/beamQuest.js index 9661a0c..a6b18ea 100755 --- a/public/js/src/beamQuest.js +++ b/public/js/src/beamQuest.js @@ -24,6 +24,8 @@ var BeamQuestWorld = cc.Layer.extend({ this.inputHandler = new InputHandler(bq.player); this.baseLayer = baseLayer; this.scheduleUpdate(); + + this.initPing_(); return true; }, @@ -45,6 +47,40 @@ var BeamQuestWorld = cc.Layer.extend({ /** @override */ onKeyUp: function(key) { this.inputHandler.keyUp(key); + }, + + initPing_: function() { + if (! cc.Director.getInstance().isDisplayStats()) { + return; + } + + // ping始める + var interval = 300; + this.ping_ = new bq.Ping(bq.Socket.getInstance().socket, interval); + this.ping_.start(); + + // 表示用テキストを作る + var pingText = function() { + return "ping: " + this.ping_.getRoundTripTime(); + }.bind(this); + + // ラベル作る + var fontSize = 16; + var pingLabel = bq.Label.createWithShadow(pingText(), fontSize); + pingLabel.setAnchorPoint(cc.p(0, 1.0)); + + // 表示位置 + var margin = 5; + var winSize = cc.Director.getInstance().getWinSize(); + pingLabel.setPosition(cc.p(margin, winSize.height - margin)); + + // 表示内容更新処理 + pingLabel.schedule(function() { + pingLabel.setString(pingText()); + }); + + var zIndex = 10000; + this.addChild(pingLabel, zIndex, bq.config.tags.DEBUG_PING); } }); diff --git a/public/js/src/config.js b/public/js/src/config.js index f7e051c..1285d7e 100755 --- a/public/js/src/config.js +++ b/public/js/src/config.js @@ -15,7 +15,8 @@ var BQConfig = cc.Class.extend({ // Spriteのタグ(IDみたいなの) this.tags = { - BASE_LAYER: 0 + BASE_LAYER: 0, + DEBUG_PING: 1000000 } }} ); diff --git a/public/js/src/ping.js b/public/js/src/ping.js new file mode 100644 index 0000000..7aa2550 --- /dev/null +++ b/public/js/src/ping.js @@ -0,0 +1,42 @@ +bq.Ping = cc.Class.extend({ + ctor: function(socket, interval) { + this.socket_ = socket; + this.interval_ = interval; + this.roundTripTime_ = 0; + this.isRunning_ = false; + }, + + start: function() { + this.isRunning_ = true; + this.sendPing_(); + }, + + stop: function() { + this.isRunning_ = false; + }, + + isRunning: function() { + return this.isRunning_; + }, + + getRoundTripTime: function() { + return this.roundTripTime_; + }, + + sendPing_: function() { + this.socket_.once('notify:ping', this.onNotifyPing_.bind(this)); + this.socket_.emit('ping:update', {time: this.now_()}); + }, + + onNotifyPing_: function(data) { + this.roundTripTime_ = this.now_() - data.time; + if (this.isRunning_) { + setTimeout(this.sendPing_.bind(this), this.interval_); + } + }, + + now_: function() { + return new Date().getTime(); + } +}); +