Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ping値を常に表示 #9

Merged
merged 5 commits into from
Dec 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/beamQuest/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var SessionStore = {
}
};

var ping = require('beamQuest/ping');

exports.start = function(io) {
var client = SessionStore;
Expand Down Expand Up @@ -42,6 +43,8 @@ exports.start = function(io) {
io.sockets.emit('notify:message', { message: data.message });
});

ping.listen(socket);

socket.emit('connected');
});
};
Expand Down
6 changes: 6 additions & 0 deletions app/beamQuest/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exports.listen = function(socket) {
socket.on('ping:update', function(data) {
socket.emit('notify:ping', data);
});
};

1 change: 1 addition & 0 deletions public/cocos2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
};
Expand Down
36 changes: 36 additions & 0 deletions public/js/src/beamQuest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var BeamQuestWorld = cc.Layer.extend({
this.inputHandler = new InputHandler(bq.player);
this.baseLayer = baseLayer;
this.scheduleUpdate();

this.initPing_();
return true;
},

Expand All @@ -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);
}
});

Expand Down
3 changes: 2 additions & 1 deletion public/js/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var BQConfig = cc.Class.extend({

// Spriteのタグ(IDみたいなの)
this.tags = {
BASE_LAYER: 0
BASE_LAYER: 0,
DEBUG_PING: 1000000
}
}}
);
Expand Down
42 changes: 42 additions & 0 deletions public/js/src/ping.js
Original file line number Diff line number Diff line change
@@ -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();
}
});